记住我想要打破只有整个脚本的情况;哪个循环更适合这个问题?
#!/bin/bash
. /home/nayanc/project/info
. /home/nayanc/project/servers
#. /home/nayanc/project/cal
echo -e "1.System Information \n2.Calculation \n3.Server Configuration \n4.Exit"
read -p "Enter Your Choice " ch
case $ch in
1 )
echo -e "a.Basic Information \nb.Intermedite Information \nc.All Information \nd.Exit from case \nExit from program"
read -p "Enter Your Choice" ch1
case $ch1 in
a )
basic
;;
b )
inter
;;
c )
myinfo1
;;
esac
;;
2 )
echo -e "a.Addition \nb.Subtraction \nc.multiplication \nd.Exit from case \ne.Exit from program"
read -p "Enter your Choice " ch2
case $ch2 in
a )
add_numbers
;;
b )
sub_numbers
;;
c )
mul_numbers
;;
d )
echo "Exit from Loop"
break 1
;;
e )
echo "Exit from program"
;;
* )
echo "Please enter correct choice"
;;
esac
;;
3 )
;;
4 )
exit 0
;;
* )
;;
esac
答案 0 :(得分:1)
break仅用于for,select,while和until循环,但不是以防万一。 但似乎你有点困惑。 默认使用;;如果终止案件而不是程序。
问题不是如何终止案例,而是如何让它一直运行直到我们想要终止它。
见这个例子:
echo -e "a.Basic Information \nb.Intermedite Information \nc.All Information \nd.Exit from case \ne.Exit from program"
read -p "Enter Your Choice" ch1
case $ch1 in
a ) echo "-->run basic information script";;
b ) echo "-->run intermediate information script";;
c ) echo "-->run allinformation script";;
d ) echo "-->Exit from case";;
e ) echo "-->Exit from program"
exit;;
* ) echo "Wrong Selection - Try Again"
esac
echo "Command X: This is command X after the case" #Command inside program but out of case
如果按a,b,c或d,则执行相应的命令内部案例,案例将终止,并且由于程序未终止,命令X外部案例也将被执行。
但是如果按e,将执行命令exit
内部情况,程序将被终止,因此最后一个命令X(外部情况)将不会被执行。
选择a,b,c或d
时的输出root@debi64:/home/gv/Desktop/PythonTests# ./bashtest.sh
a.Basic Information
b.Intermedite Information
c.All Information
d.Exit from case
e.Exit from program
Enter Your Choice : a
-->run basic information script
Command X : This is command X after the case
root@debi64:/home/gv/Desktop/PythonTests#
#If you select e, the last command X is not executed since program is terminated due to exit.
如果你想让案件保持活着直到你选择(按d
),那么你可以在案件之前使用while循环:
while [ "$ch1" != "d" ];do
echo -e "a.Basic Information \nb.Intermedite Information \nc.All Information \nd.Exit from case \ne.Exit from program"
read -p "Enter Your Choice : " ch1
case $ch1 in
a ) echo "-->run basic information script";;
b ) echo "-->run intermediate information script";;
c ) echo "-->run allinformation script";;
d ) echo "-->Exit from case";;
e ) echo "-->Exit from program"
exit;;
* ) echo "Wrong Selection - Try Again"
esac
done #if d or e are not pressed the menu will appear again
echo "Command X: This is command X after the case"
在上述情况/组合中,如果您选择a
,b
或c
将执行相应的案例命令,将不执行命令X并重新加载案例菜单。
如果按d
,情况将终止并且命令X将被执行,因为程序未终止。
如果按e
,则案例和程序将exit
,并且程序终止后将不会执行命令X.
或者你可以使用select&案例,类似于while&情况下:
IFS=$'\n' #necessary since select by default separates option using space as a delimiter
op=( "Basic Information" "Intermedite Information" "All Information" "Exit from case" "Exit from program" ) #Use an array
select ch1 in ${op[@]}; do
echo "You choose $ch1"
case $ch1 in #or case $ch1 in
"Basic Information" ) echo "-->run basic information script";; #${op[0} )....;;
"Intermedite Information" ) echo "-->run intermediate information script";; #${op[1} )....;;
"All Information" ) echo "-->run allinformation script";; #${op[2} )....;;
"Exit from case" ) echo "-->Exit from case" #${op[3} )....;;
break;;
"Exit from program" ) echo "-->Exit from program" #${op[4} )....;;
exit;;
* ) echo "Wrong Selection - Try Again"
esac #esac
done
echo "Command X: This is command X after the menu"
再次按“退出大小写”,大小写和选择将被终止,并且选择后的命令X将被执行,因为程序没有终止。