2016-12-16 78 views
0

记住我只想打破案件不是整个脚本;哪个循环更适合这个问题?如何突破案例并继续其他循环

#!/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

你的脚本必须有一个分隔符才能退出?没有该脚本不能终止 – Inian

回答

0

break仅用于for,select,while和until循环,但并非如此。 但似乎你有点困惑。 默认使用;;如果案件终止而不是程序。

问题不在于如何终止case,而是如何让它继续运行,直到我们想终止它。

见下面的例子:

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

[email protected]:/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 
[email protected]:/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" 

在上述情况下/组合而如果选择abc相应情况下的命令将被执行,命令X将不会被执行和壳体菜单将被重新加载。

如果按d,则情况将终止,并且由于程序没有终止,Command X将被执行。

如果您按e,案例和程序将exit和命令X将不会执行,因为程序终止。

另外,您可以使用select &情况下,这是类似消磨&情况:

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由于程序没有终止而被执行。