I'm using GNU bash, version 4.3.46 on a Ubuntu machine. For some reason this while loop doesn't work as expected.
The menu should loop continuously until the user decides to quit the program, then there is some error checking prompting the user if they are sure, then the program ends.
Here's the code:
#!/bin/bash
menu_choice=0
quit_program="n"
while [[ $menu_choice -ne 3 && $quit_program != "y" ]]
do
printf "1. Backup\n"
printf "2. Display\n"
printf "3. Exit\n\n"
printf "Enter choice: \n"
read menu_choice
if [ $menu_choice -eq 3 ]
then
printf "Are you sure you want to quit? (y/n)\n"
read quit_program
fi
done
I think it might have to with global variables are declared at the beginning and I'm reading in a new value locally...
&&
for the conditions in the while loop. You want it to loop as long as either is true, not both at once. Try using||
instead. – TheWanderer Mar 26 '17 at 18:46select... case
construct instead: see How can I create a select menu in a shell script? – steeldriver Mar 26 '17 at 18:52menu_choice
variable at all for the while loop. See my answer. – Delorean Mar 26 '17 at 19:08