1

I am facing the syntax error near unexpected token,elif error for the following code.

if [ "$1" == "abc" ]; then

   echo "abc"

elif [ "$1" == "xyz" ]; then

   echo "xyz"

else 

    echo "Unkown parameter"
    exit 0

fi

Error is: abc.sh: line 28: syntax error near unexpected token elif' abc.sh: line 28:elif [ "$1" == "xyz" ]; then

3
  • This error has been discussed. Please search in previous posts. Commented Jul 23, 2015 at 12:23
  • I have run that code and I'm not getting any errors: $ cat sh.sh #!/bin/bash if [ "$1" == "abc" ]; then echo "abc" elif [ "$1" == "xyz" ]; then echo "xyz" else echo "Unkown parameter" exit 0 fi $ ./sh.sh abc abc $ ./sh.sh xyz xyz $ ./sh.sh a Unkown parameter Commented Jul 23, 2015 at 12:39
  • Does using = instead of == help? (POSIX specifies = bash/etc. allow == for compatibility though.) If not then this code snippet is valid and the error lies somewhere else and/or in something that didn't paste cleanly (DOS line endings, odd non-printing characters, etc.). Commented Jul 23, 2015 at 12:44

1 Answer 1

0

Code seems alright but only point I would like to add is the OS on which you are running. The == works fine with Linux RedHat/Suse or some solaris machine but on some OS like Hpux or AIX it don't work. You should use = which is correct as you are comparing strings.

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.