0

I'm a beginner to shell scripts, and wrote below script but it keeps returning that "done" is not recognized token.

I know this is a super simple script, but I am too novice to debug this.. could anyone explain why this is not working? Thanks!

  #!/bin/sh

    for file in `ls`
    do
    if [ -f $file ]
            echo $file + " exists"
    fi
    done

comment: added fi, but still has an error: syntax error near unexpected token `fi'

4
  • but shell script doesn't require semicolons. Commented Jul 27, 2015 at 2:00
  • Also, I don't think $file + " exists" will output what you expect, as it will print a literal "+" sign. You can simply write: echo $file exists. Or, if you want to make the message more explicit: echo "$file exists". Commented Jul 27, 2015 at 2:06
  • @dsetton That's right, thanks! Commented Jul 27, 2015 at 2:10
  • @pandagrammer You may find shellcheck useful. It tries to give more helpful error messages for these things. Commented Jul 27, 2015 at 3:41

1 Answer 1

2

You miss the then (and your echo statement is probably malformed). Try:

#!/bin/sh

for file in `ls`
do
  if [ -f $file ]
  then
    echo "$file exists"
  fi
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.