具有以下shell,其中(while true)循环内部(while true)循环。我试图用“休息”打破内循环,但事实并非如此。 我想打破内循环&显示用户第一循环的选项;建议请。
#!/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
以下是输出:
~~~~~~~~~~~~~~~~~~~~~
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
答案 0 :(得分:7)
以下脚本适用于此。
如果read_sub_options
被调用,则sub_menu
返回2,当选择返回选项时返回1,然后检查showSubMenu中最后一次运行命令的返回值,如果返回则返回地选择。
有关更多KSH脚本参考信息,请参阅KSH script basics和Learning the Korn shell。
有关您的问题评论中提到的其他菜单选择方法的信息,请参阅select和ksh手册页。
#!/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
答案 1 :(得分:1)
shell“始终”支持:
break 2
打破两个级别的shell循环。即使是第7版UNIX™Bourne shell也是如此。
答案 2 :(得分:0)
您正试图从该函数内部的函数外部循环。我不完全清楚这是否应该有效,但我发现它不适用于ksh93,mksh和Heirloom Bourne shell,而它适用于bash,ash(如dash和FreeBSD sh)和zsh。 / p>
您可以返回一个特殊状态,并在循环中执行相同的功能,就像在其他一个答案中一样。