2014-01-11 32 views
0

我有這個問題,我正在做一個菜單排序的程序,用戶應該輸入不同的數字,具體取決於要執行的操作。 現在我需要將多個標誌放在一個命令中,但我似乎無法使其工作。 美中不足的是:我不能在標誌直接鍵入到一個變量,我可能不得不有這樣的情況下,列表是可以隨意退出)BASH腳本:取得多個標記

function fileCpy { 
    printf "Choices:\n1. Interactive copy, answer yes/no before doing the copy: \n2. Make backups of existing destination files\n3. Preserve file attributes\n4. Do a recursive copy\n0. Return to main menu\n" 
     #read choices 
    read $a 
    read $b 
    read $c 
    for choices in $choice ; do 
     case "$choices" in 
      1) 
     a=-i ;; 
      2) 
     b=--backup ;; 
      3) 
     c=-p ;; 
      #4) 
     #4= -R -v;; 
      0) 
     main;; 
    esac 
    echo "$a" 
    echo "$b" 
    echo "$c" 
    printf "\nType the name of the file you wish to copy/backup: " 
    read $fl1 
    printf "\nType the destination file: " 
    read $des1 
    cp $a $b $c $fl1 $des1 
    done 
} 

回答

0

,如果你想要做的菜單,你可以使用select結構(或case/esac)。見here的信息

1

一個問題是,read $a等是錯誤的:如果你需要讀取,你應該寫0123'。就目前而言,讀取的值將存儲在名稱存儲在$a中的變量中。

另一個問題是,對於無辜的用戶來說,他們應該在劇本繼續之前輸入3行信息,但三條read強制執行這條線,這是很不清楚的。

另一個問題是,你不讀$choice(通過read choice)所以for循環無關。

另一個問題是您的腳本將繼承任何恰好與您所使用的變量名稱相同的環境變量的值。

另一個問題是你不引用文件名。除非你有一個包含空格或其他類似尷尬的字符的名字,否則它並不重要。

整容問題是,printf陳述是可笑的長。每行使用一個printf。或使用echo。滾動頁面RHS的東西很糟糕(雖然我不認爲80行是固定長度的行,但對於長度大於80的行會有二次懲罰 - 因爲(長度爲80)增加,長線的痛苦增加

在另一個層面上,界面是謙虛怪誕的,作爲一個shell腳本的練習,它是有道理的,作爲一個練習如何設計好的shell腳本,它是一個非常。不好的設計

一個設計,可能是有意義的是:

  1. 設置變量爲空:a=""; b=""; c="";
  2. 提供一系列類似於現在給出的選擇,但添加一個選項來執行命令,另一個選項放棄發貨。
  3. 有一個讀取選項並設置標誌的循環。
  4. 當用戶選擇執行時,退出循環並提示輸入文件名。
  5. 如果一切正常,請執行命令。

請注意,您應該檢查read命令的工作;如果他們不這樣做,安全失敗(不要損害任何東西)。


把所有的一起(有一些細微的差別,但同樣的整體效果 - 見證變量使用local):

fileCpy() 
{ 
    local a b c file dest 
    echo "Choices:" 
    echo "0. Return to main menu" 
    echo "1. Interactive copy, answer yes/no before doing the copy" 
    echo "2. Make backups of existing destination files" 
    echo "3. Preserve file attributes" 
    echo "4. Do a recursive copy" 
    echo "5. Execute the copy" 
    while printf "Your choice: " && read choice 
    do 
     [ -z "$choice" ] && return 1 # Empty input - failure 
     case "$choice" in 
     (0) return 0;; 
     (1) a="-i";; 
     (2) b="--backup";; 
     (3) c="-p";; 
     (4) d="-R";; 
     (5) break;; 
     (*) echo "Unrecognized response ($choice); please enter 0..5";; 
     esac 
    done 
    [ "$choice" != 5 ] && return 1 # EOF - failure 
    printf "Type the name of the file you wish to copy/backup: " 
    read file 
    [ -z "$file" ] && return 1 # Empty file name - failure 
    printf "Type the name of the destination file/directory: " 
    read dest 
    [ -z "$dest" ] && return 1 # Empty file name - failure 
    cp $a $b $c "$file" "$dest" 
} 

測試代碼:

echo "a=$a b=$b c=$c file=$file dest=$dest" 
if fileCpy 
then : OK 
else echo "Failed" 
fi 
echo "a=$a b=$b c=$c file=$file dest=$dest" 

最後一塊是一個簡單的測試工具。它報告函數中使用的變量的值,運行函數,報告函數是否失敗,並重新回顯變量以證明它們沒有被設置。

除非您支付我這樣做,否則我不會使用該界面,但它或多或少地符合培訓練習的目標。

+0

感謝您的輸入,但我確實有一些問題: 點3的循環如何看起來像?這基本上是我不知道的。 其次,應該以cp $ a $ b $ c $ fl1 $ des1的結尾有效嗎? 我目前正在獲取的錯誤(在做了一些修改後,即在您的示例之後更改讀取變量並在每種情況下在文本字符串周圍添加「」。 – Skurt