1
votes

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

1
This error has been discussed. Please search in previous posts. - Shravan Yadav
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 - bob dylan
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.). - Etan Reisner

1 Answers

0
votes

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.