2017-07-28 53 views
0

我在腳本中使用以下代碼。Bash - 具有多個選項的閱讀案例

#!/bin/bash 

while true; do 
    read -p "Do your Choice: [1] [2] [3] [4] [E]xit: " choice 
    case "$choice" in 
     [1]*) echo -e "$choice\n"; break;; 
     [2]*) echo -e "$choice\n"; break;; 
     [3]*) echo -e "$choice\n"; break;; 
     [4]*) echo -e "$choice\n"; break;; 
     [Ee]*) echo "exited by user"; exit;; 
     *) echo "Are you kidding me???";; 
    esac 
done 

我的問題是,我怎麼能拿到劇本接受多個選擇。 所以輸入像:1,4,將運行案例[1][4]

回答

2

集IFS包括逗號:

IFS=', ' 

然後處理該選擇在一個循環中(注意-a標誌read所以輸入被視爲一個陣列):

while true; do 
    read -p "Do your Choice: [1] [2] [3] [4] [E]xit: " -a array 
    for choice in "${array[@]}"; do 
     case "$choice" in 
      [1]*) echo -e "$choice\n";; 
      [2]*) echo -e "$choice\n";; 
      [3]*) echo -e "$choice\n";; 
      [4]*) echo -e "$choice\n";; 
      [Ee]*) echo "exited by user"; exit;; 
      *) echo "Are you kidding me???";; 
     esac 
    done 
done 
+1

我想OP的代碼使用'break'從'while'循環中退出。 – Jdamian

+0

好點,我會解決這個問題。 – yinnonsanders