I wrote a bash-driven menu-driven script that uses a switch case inside a while to execute various menu options. Everything works perfectly. Now I am trying to improve the program by performing error checking on the user input, but I cannot get it to work ...
The problem is that I don’t know how to break out of the switch statement correctly without breaking out of the while loop (so the user can try again).
# repeat command line indefinitely until user quits while [ "$done" != "true" ] do # display menu options to user echo "Command Menu" # I cut out the menu options for brevity.... # prompt user to enter command echo "Please select a letter:" read option # switch case for menu commands, accept both upper and lower case case "$option" in # sample case statement a|A) echo "Choose a month" read monthVal if [ "$monthVal" -lt 13 ] then cal "$monthVal" else break # THIS DOES NOT WORK. BREAKS WHILE LOOP, NOT SWITCH! fi ;; q|Q) done="true" #ends while loop ;; *) echo "Invalid option, choose again..." ;; esac done exit 0
The program works fine when the user enters a valid month value, but if they enter a number above 13, instead of breaking the switch statement and repeating the cycle again, the program breaks both the switch and the while loop and stops working.
source share