0

I'm coding a nestedif program as shown below but there is a syntax error that says "Syntax error: "then" unexpected (expecting "fi")

#!bin/sh

choose=0
echo "Do you want to choose a color?"

read choose

if [ $choose == "Y" ] ; then

echo "1. Blue"

echo "2. Green"

echo -n "Select your choice [1 or 2]? "

read choice

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

        echo "you chose blue"
 else [ $choice == "2" ] ; then

        echo "you chose green"
 fi

else     

 echo " invalid choice"

fi

I expect it to be if i choose 1 the output will be blue then if i choose 2 the output will be green else invalid choice

5
  • 1
    Whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask. Commented May 9, 2019 at 1:40
  • Some suggestions. (1) Recognize "y" as well as "Y", and have the prompt specify what input is expected. (2) Put double quotes around your variable references to avoid errors when the input is empty: if [ "$choose" == "Y" ] ; then .... (3) Indent your code consistently. Commented May 9, 2019 at 2:13
  • 1
    When the user has more choices, consider case "{choice}" in ... esac. Commented May 9, 2019 at 8:17
  • "Invalid choice" is an error message, and should be written to stderr. echo 'invalid choice' >&2 Commented May 17, 2019 at 20:54
  • You should also have #!/bin/sh instead of #!bin/sh Commented May 17, 2019 at 22:53

1 Answer 1

2

Replace:

else [ $choice == "2" ] ; then

with:

elif [ "$choice" = "2" ] ; then

Notes:

  1. else is used for the final clause, one without conditions, in an if statement. Use elif if you want to apply a test, such as, in this case, [ "$choice" = "2" ].

  2. Always put shell variables in double-quotes unless you explicitly want the shell to apply expansions such as word splitting and pathname expansion.

  3. == inside [...] is not POSIX. Bash accepts it but other shells, such as dash, don't. For greater portability, use =.

Sign up to request clarification or add additional context in comments.

6 Comments

Worked but it said another error which is " ./try.sh: 6: [: y: unexpected operator
Did you try using shellcheck.net? It will save you a lot of time.
@qaqu: When I copy-and-paste the script from your question and make this change, it works. Also, line 6 is read choose; there's no "y" on that line. You must have been using a script that differs from the one you posted.
Better yet, use else [ "$choice" = "2" ]; . == is a non-standard operator
@WilliamPursell Good observation. Answer updated to mention that == inside [...] is not supported by the POSIX standard.
|

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.