0

first time asking a question here and still learning bash, so bear with me.

I made a main screen using while and case, it was supposed to work but it's not and I don't know where I made a mistake.


while [ $WH!=5 ]
do

echo "Choice menu:"
echo "          1) Option 1"
echo "          2) Option 2"
echo "          3) Option 3"
echo "          4) Option 4"
echo "          5) End script"
echo -n "Choose an option: "
read $OP

case $OP in
        1)
            # code;;

        2)
            # more code;;

        3)
            # more code;;

        4)
            # even more code;;

        5)
            echo "Thank you for testing this script!"
            $WH=5;;
esac
done

No matter what I put in $OP, the script keeps repeating the choice menu until I cancel it. I tried moving while and using $OP as the condition, but nothing seemed to work.

6
  • It has to be read OP, not read $OP. Commented Nov 23, 2016 at 16:51
  • 1
    The while condition is incorrect, too (needs spaces around !=) - shellcheck.net will tell you things like that. Commented Nov 23, 2016 at 16:52
  • This might help: How to debug a bash script? Commented Nov 23, 2016 at 17:06
  • By the way, have a look at the select statement to build simple menus like this. Commented Nov 23, 2016 at 17:07
  • Replace $WH=5 by WH=5 and !" by "! and read $OP by read OP. Commented Nov 23, 2016 at 17:07

1 Answer 1

2

You have multiple issues in your script:-

  1. while construct needs to be while [ "$WH" != 5 ]. Remember to double-quote all your shell variables.
  2. The read command should be read OP
  3. The case constructs need to atleast one line with a ending ;;
  4. The final assignment statement should be WH=5 and not $WH=5

Use https://www.shellcheck.net to debug such trivial issues.

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

1 Comment

Some general rules: use $ to get variables' values, not when setting their values; use double-quotes around variable references ("$WH" instead of $WH); and spaces matter a huge amount in shell (e.g. around !=). Oh, and one more: it's best to use lowercase variable names, do avoid conflicts with the variables that have special meaning to the shell and/or other programs (those are all caps).

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.