0

I am a beginner at unix and this is my first for loop with if statement. I am getting an error which says syntax error near unexpected token 'then'. Please can you help me fix this. What have i done wrong.

for file in $@
do
    if [[ ! -f $@ ]]
    then
           echo "Error. File does not exist."
           exit $ERROR_NO_FILE

    elif
    then
           chmod 755 $file
           echo "File permissions have been changed."
           exit $SUCCESS
    fi
done
8
  • 1
    [[ ! -f $@ ]]? Eh? -f can't take an arbitrary number of arguments, and you don't know how many things $@ will expand to. You want [[ ! -f "$file" ]] (lack of quoting acceptable there because of side effects of [[ ]], though it still doesn't hurt). Commented Dec 2, 2016 at 21:35
  • Also, you want for file in "$@", chmod 755 "$file", etc; -- as it is, this will break badly with filenames with spaces. Run your code through shellcheck.net and fix what it finds. Commented Dec 2, 2016 at 21:35
  • 1
    DO NOT CHANGE YOUR QUESTION IN A WAY THAT INVALIDATES PREEXISTING ANSWERS. Commented Dec 2, 2016 at 21:37
  • I was wondering why I had to edit the answer for correctness. Now I know, and reverting to the old version. Sorry! Commented Dec 2, 2016 at 21:38
  • Do not write for f in $@. Quotes are important. It should be either for f in "$@"; do... or for f; do ... unless you have a very special use case and know the difference and you actually want an unquoted $@. In that case you will want to add a comment explaining exactly what motivates the unusual usage. Commented Dec 2, 2016 at 21:45

1 Answer 1

1

there is an extra elif/then in the middle. Try replacing it with an else

       exit $ERROR_NO_FILE

else
       chmod 755 $file
Sign up to request clarification or add additional context in comments.

1 Comment

@pogba123, because you still have the extra then; you aren't doing exactly what this answer tells you to do.

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.