2016-02-10 63 views
0

我正在使用腳本來備份我的CouchPotatoServer,但我遇到問題。選擇一個選項後沒有完成選擇

這裏是我有問題的代碼:

select OPTION in Backup Restore Finish; do 
    echo "You choose $OPTION CouchPotatoServer settings"; 
    case $OPTION in 
     Backup) 
      echo "Backing up settings file to $CPBPATH:"; 
      cp $CPSPATH/settings.conf $CPBPATH/settings-"$(date +%Y%m%d-%H%M)".bak ; 
      echo "Done!" 
      break 
      ;; 
     Restore) 
      echo "Please choose a backup to restore settings" ; 
      AVAILABLEFILES="($(find $CPBPATH -maxdepth 1 -print0 | xargs -0))" 
      select FILE in $AVAILABLEFILES; do 
       cp "$FILE" $CPSPATH/settings.conf ; 
       echo "restored $FILE" 
       break 
       ;; 
done 

的問題是,用戶選擇一個選項並執行代碼之後,就一直等待一個新的選擇,但我希望它出口。我怎樣才能做到這一點?

+0

您錯過了'esac'來關閉'case'語句。 – chepner

回答

1

break退出一個循環,但你有嵌套循環,並卡在外面的一個。 break實際上需要一個參數來指定要退出的封閉循環的次數,因此當您將break替換爲break 2時,您還將退出外圍select循環。

這裏是一個小的腳本來演示select語句不同break等級:

#!/bin/bash 

PS3="Outer selection: " 
select option1 in outer1 outer2 ; do 
    echo "option1 is $option1" 
    PS3="Inner selection: " 
    case "$option1" in 
     outer1) 
      select option2 in inner1 inner2; do 
       echo "option2 is $option2, issuing 'break'" 
       PS3="Outer selection: " 
       break 
      done 
      ;; 
     outer2) 
      select option2 in inner3 inner4; do 
       echo "option2 is $option2, issuing 'break 2'" 
       break 2 
      done 
      ;; 
    esac 
done 

PS3是使用select語句時顯示的提示。只要外部選項是outer1,您將循環回外部select,因爲只發佈一個break;如果您選擇outer2,您將退出程序break 2

相關問題