Okay, so I have this simple menu-driven BASH script. I’m still learning BASH, so “KISS” is my method, for the moment. The code below works, so that’s cool … take a look, i’ll explain my issue on the other side …
echo -en "nWhat type of server is this? [DIGIT ONLY]nn1. cPaneln2. Pleskn3. Ubuntun4. No Panel / Simple Control PanelnnEnter Selection: "
read type
while true; do
case $type in
"1")
echo -en "Here is the stuff we can do for a cPanel servernn1. do thisn2. do thatnnEnter Selection: ";
read cpopt
case $cpopt in
"1")
echo "sub1"
break
;;
"2")
echo "sub2"
break
;;
*)
echo "You did not choose an option, lets go back!"
break
;;
esac;;
"2")
echo "Plesk Stuff";
break
;;
"3")
echo "Ubuntu Stuff";
break
;;
"4")
echo "No Panel / Simple Control Panel";
break
;;
*)
echo "Invalid Input";
break
;;
esac
done
As you can see, each CASE statement works (so that’s good), but here is what I’m trying to do … I want to create a simple navigation system … something that allows users 5-Levels deep to go back one level (or quit altogether). As you can see, I’m using BREAK, but that quits the whole script, so then I thought I would use a nested WHILE, thinking the BREAK would only break out of the loop it was in (the nested one) … no joy, that also killed the whole script.
Since I’m learning, I’m not here looking for a definitive answer (although those are nice) … I’m really looking for “You should read about XYZ, that will do that) … and then I’ll dive into that.
I very much appreciate any help that is offered, thank you!
2
Answers
Here is the new, perfectly navigated code:
Thanks, I really appreciate the help!
You have nested cases but not looped them so it will just carry on the first case statement after the second
Something like this should work
You will need to create a new loop for each sub menu and also omit the
breaks
from options you want to loop like and incorrectly selected option.Furthermore if you want to move up multiple level of loops(or menus) then you can put a number after the
break
i.e
This will move back through two levels of loops
For further reading on loop control you can look here
http://tldp.org/LDP/abs/html/loopcontrol.html