Have the following shell, which have a (while true) loop inside a (while true) loop. I am trying to break the inner loop using a "break" but it isn't. I want to break the inner loop & display user the options of 1st loop; suggestions pls.
#!/bin/ksh
sub_menu() {
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " S U B - M E N U "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "1. Display properties"
echo "2. Back"
}
read_sub_options(){
echo "Please select option"
read option
case $option in
1) sub_menu ;;
***2) break ;;***
*) echo "Please insert options 1 ~ 2";;
esac
}
showSubMenu(){
while true
do
sub_menu
read_sub_options
done
}
read_options(){
echo "Please select option "
read option
case $option in
1) showSubMenu ;;
2) exit 0;;
*) echo "Please insert options ";;
esac
}
show_menus() {
echo "~~~~~~~~~~~~~~~~~~~~~"
echo " M A I N - M E N U "
echo "~~~~~~~~~~~~~~~~~~~~~"
echo "1. Sub Menu"
echo "2. Exit"
}
# -----------------------------------
# MAIN
# ------------------------------------
while true
do
show_menus
read_options
done
Here is the output:
~~~~~~~~~~~~~~~~~~~~~
M A I N - M E N U
~~~~~~~~~~~~~~~~~~~~~
1. Sub Menu
2. Exit
Please select option
1
~~~~~~~~~~~~~~~~~~~~~~~~~
S U B - M E N U
~~~~~~~~~~~~~~~~~~~~~~~~~
1. Display properties
2. Back
Please select option
2
~~~~~~~~~~~~~~~~~~~~~~~~~
S U B - M E N U
~~~~~~~~~~~~~~~~~~~~~~~~~
1. Display properties
2. Back
Please select option
2
Breaking from a while LoopUse the break statement to exit a while loop when a particular condition realizes. The following script uses a break inside a while loop: #!/bin/bash i=0 while [[ $i -lt 11 ]] do if [[ "$i" == '2' ]] then echo "Number $i!" break fi echo $i ((i++)) done echo "Done!"
In the following example, we are using the built-in command : to create an infinite loop. : always returns true. You can also use the true built-in or any other statement that always returns true. The while loop above will run indefinitely. You can terminate the loop by pressing CTRL+C .
break is a special built-in shell command. In the tcsh shell, break causes execution to resume after the end of the nearest enclosing foreach or while. The remaining commands on the current line are executed. Multilevel breaks are thus possible by writing them all on one line.
While loop is used to execute a block of code repeatedly until given boolean condition evaluated to False. If we write while True then the loop will run forever.
The following script works here.
The changes made were to have read_sub_options
return 2 if sub_menu
was called and return 1 when Back option was selected and then check the return value of the last run command in showSubMenu and break if Back was selected.
See KSH script basics and Learning the Korn shell for further KSH scripting reference information.
See select and the ksh man page for information on an alternate method of menu selection as mentioned in the comment of your question.
#!/bin/ksh
sub_menu() {
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " S U B - M E N U "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "1. Display properties"
echo "2. Back"
return 2;
}
read_sub_options(){
echo "Please select option"
read option
case $option in
1) sub_menu ;;
2) return 1;;
*) echo "Please insert options 1 ~ 2";;
esac
}
showSubMenu(){
while true
do
sub_menu
read_sub_options
if [[ $? == 1 ]] ; then
break
fi
done
}
read_options(){
echo "Please select option "
read option
case $option in
1) showSubMenu;;
2) exit 0;;
*) echo "Please insert options ";;
esac
}
show_menus() {
echo "~~~~~~~~~~~~~~~~~~~~~"
echo " M A I N - M E N U "
echo "~~~~~~~~~~~~~~~~~~~~~"
echo "1. Sub Menu"
echo "2. Exit"
}
# -----------------------------------
# MAIN
# ------------------------------------
while true
do
show_menus
read_options
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With