0
#!/bin/sh
pwd=`pwd`
echo $#
if [ $# -gt 0 ]
then
    echo $#
    echo $1
    rm `ls -ls $1 | awk '$1 == "0" {print $10}'`
else
    echo $#
    echo $pwd
    rm `ls -ls $pwd | awk '$1 == "0" {print $10}'`
fi

output

-bash-4.2$ sh plead_the_fifth.sh
0
plead_the_fifth.sh: line 13: syntax error near unexpected token `fi'
plead_the_fifth.sh: line 13: `fi'

I have tried to echo everything but I couldn't get fi to work. I've tried for about an hour or two to no avail.

  • 1
    Your script might have DOS/Windows-style line endings; see ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) (answer: yes, very). Also, [parsing `ls` output is a bad idea](https://mywiki.wooledge.org/ParsingLs); it'd be much safer to use [`find`](https://mywiki.wooledge.org/UsingFind#Searching_based_on_sizes) (probably with `-maxdepth 1` so it doesn't search subdirectories). – Gordon Davisson Apr 12 '22 at 04:46
  • The entire `if`/`then` could be simplified to `rm -f "$(ls "${1-.}" | awk '$1 == "0" {print $10}')"`; notice the addition of quoting, and the use of modern command substitution syntax. But I concur with the previous comment; your code as posted here does not contain a syntax error, and you should definitely avoid using `ls` in scripts. – tripleee Apr 12 '22 at 05:15
  • @Gordon Davisson yes that was it although the reason I don't have to find it in the script is that it was the professor that said I couldn't use find – Nimball Dimpall Apr 12 '22 at 05:42
  • @tripleee yes I definitely could use that. also as I said in my comment to Gordon Davisson for the assignment i could not use find – Nimball Dimpall Apr 12 '22 at 05:48
  • @NimballDimpall If you can't use `find`, you could also use a plain wildcard to get the filenames (e.g. `for file in "$1"/*; do`) and `stat` to get each file's size. – Gordon Davisson Apr 12 '22 at 07:00
  • @GordonDavisson yes the for loop would work but how would you find if there was a directory given in the argument – Nimball Dimpall Apr 12 '22 at 14:49
  • @NimballDimpall Use something like `if [ -d "$1" ]; then` – Gordon Davisson Apr 12 '22 at 16:51

0 Answers0