2016-12-16 29 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由於程序沒有終止而被執行。