Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Statement not matching "*"

Tags:

bash

I am writing a bash script. It is menu driven.

For some reason, i use a case statement, and after all my options are printed, i use the * to capture anything else the user may type. but for some reason, my output is not being done.

Example:

while [ 1 ]; do
       if [ $MAIN_MENU -eq 1 ]; then
       printMainMenu
       read option
       case "$option" in
            "1" ) printDiskSpace;;
            "2" ) printFreeMemory;;
            "0" ) exit;;
            "*" ) echo "Input not understood.";;
       esac
       fi
done

Now what happens is when I use the menu, if i type 1, 2, or 0 it does the proper thing. but if i typed 55 for instance, it would just redisplay the menu without echoing "input not understood".

Am i missing something here?

Thanks!

like image 967
Dan Avatar asked Oct 24 '25 03:10

Dan


1 Answers

Use *) not "*") for Bash default cases.

So, in your example:

 *) echo "Input not understood.";;
like image 166
Randy Howard Avatar answered Oct 25 '25 18:10

Randy Howard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!