Bash技巧:詳解用 case 複合命令的用法,匹配特定模式進行處理

在 Linux 的 Bash shell 中, case 複合命令(compound command)能夠在匹配特定的模式時,執行相應的命令。查看 man bash 裏面對 case 命令的說明以下:shell

case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
A case command first expands word, and tries to match it against each pattern in turn The word is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, process substitution and quote removal.

Each pattern examined is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, and process substitution. When a match is found, the corresponding list is executed. bash

If the ;; operator is used, no subsequent matches are attempted after the first pattern match. Using ;& in place of ;; causes execution to continue with the list associated with the next set of patterns. Using ;;& in place of ;; causes the shell to test the next pattern list in the statement, if any, and execute any associated list on a successful match.code

The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in list.ci

注意case 這個關鍵字是 bash 的內置命令,該命令要求最後一個參數必須是 esacesac 關鍵字自身並非 bash 的內置命令,它僅僅只是 case 命令要求必須提供的一個參數而已。rem

下面以一個 testcase.sh 腳原本舉例說明 case 命令的詳細用法,腳本內容以下:字符串

#!/bin/bash

var=4
case $1 in
    1) echo "Your input is 1." ;;
    2 | 3 | 4)
        echo "Your input is 2, or 3, or 4."
        ;;&
    $var)
        echo "Sure, your input is 4."
        ;;
    lion)
        echo "Your input is lion."
        ;;
    "big lion")
        echo "Your input is big lion."
        ;&
    "fall through from big lion")
        echo "This is fall through from big lion."
        ;;
    *)
        echo "Your input is not supported."
        ;;
esac

執行這個腳本的結果以下:get

$ ./testcase.sh 1
Your input is 1.
$ ./testcase.sh 2
Your input is 2, or 3, or 4.
Your input is not supported.
$ ./testcase.sh 4
Your input is 2, or 3, or 4.
Sure, your input is 4.
$ ./testcase.sh lion
Your input is lion.
$ ./testcase.sh "big lion"
Your input is big lion.
This is fall through from big lion.
$ ./testcase.sh tiger
Your input is not supported.

對這個執行結果和腳本的關鍵點說明以下:input

  • case 命令後面跟着的參數是被匹配的模式,這裏用 $1 來獲取執行腳本時傳入的第一個參數。在 in 參數跟着能夠處理的模式列表,每一項用 ) 做爲結尾。
  • 能夠用 2 | 3 | 4) 這樣的形式來匹配多個模式,每一個模式用 | 隔開。若是寫爲 1) | 2) | 3) 的形式會報錯。即,只有最後一個模式才用 ) 來結尾。
  • 模式列表不限於數字,能夠是不帶引號的字符串、帶引號的字符串、bash 的擴展語句、通配符,等等。例如上面使用了 $var 來匹配 var 變量的值。
  • 若是要匹配的字符串帶有空格,必定要用引號括起來,不然會報錯。
  • 能夠使用 * 通配符來表示匹配任意內容,相似於默認分支語句,這個語句必定要寫在最後面,不然會先匹配到它,影響它後面語句的匹配。
  • 每一個模式處理完以後,必定要用 ;;;&、或者 ;;& 來結尾。若是這三個都沒有提供則會報錯。
  • ;; 表示再也不往下匹配,會結束整個 case 命令的執行。做用相似於在 select 中執行 break 命令。
  • ;& 表示繼續執行下面一個模式裏面的語句,不檢查下一個模式是否匹配。上面的 "big lion") 模式使用了 ;& 結尾,從執行結果能夠看到,它會往下執行 "fall through from big lion") 模式的語句。
  • ;;& 表示繼續往下匹配,若是找到匹配的模式,則執行該模式裏面的語句。上面的 2 | 3 | 4) 模式使用了 ;;& 結尾,當匹配到 2 時,它繼續往下匹配,中間沒有找到匹配項,一直到 *) 才匹配,執行了 *) 模式裏面的語句。當匹配到 4 時,往下匹配到了 $var) 模式,而後 $var) 模式裏面用 ;; 結束執行,沒有再往下匹配。

在實際應用中,能夠把 case 命令和 getopts 命令結合使用,getopts 命令獲取執行腳本時傳入的選項參數,case 命令根據不一樣的選項參數進行不一樣的處理。一個簡單的示例以下:it

while getopts "bf" arg; do
    case ${arg} in
        b) handle_option_b ;;
        f) handle_option_f ;;
        ?) show_help ;;
    esac
done

也能夠把 select 命令 和 case 命令結合使用,select 命令獲取用戶選擇的項,case 命令根據用戶選擇的不一樣項進行不一樣的處理。這裏再也不舉例。io

相關文章
相關標籤/搜索