Bash technique: using case compound command to match specific pattern for processing


In the bash shell of Linux,caseCompound command can execute the corresponding command when matching a specific pattern. Check the case command description in man bash as follows:

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.

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.

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

Be carefulcaseThis keyword is bash’s built-in command, which requires that the last parameter must beesacesacThe keyword itself is not bash’s built-in command, it’s justcaseThe command requires only one parameter that must be provided.

Here’s atestcase.shScript to illustratecaseThe detailed usage of the command is as follows:


case $1 in
    1) echo "Your input is 1." ;;
    2 | 3 | 4)
        echo "Your input is 2, or 3, or 4."
        echo "Sure, your input is 4."
        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."

The result of executing this script is as follows:

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

The key points of the execution result and script are as follows:

  • staycaseThe parameter following the command is the matched pattern, which is used here$1To get the first parameter passed in when executing the script. stayinThe parameter is followed by a list of modes that can be processed, each of which uses the)As the end.
  • Can use2 | 3 | 4)In this way, we can match multiple patterns. Each pattern uses|Separate. If written as1) | 2) | 3)The form of the will report an error. That is, only the last mode is used)Come to an end.
  • The list of patterns is not limited to numbers. It can be quoted strings, quoted strings, bash extended statements, wildcards, and so on. For example, we used$varTo match the value of the VaR variable.
  • If you want to match a string with spaces, be sure to enclose it in quotation marks, otherwise an error will be reported.
  • have access to*Wildcard to match any content, similar to the default branch statement, this statement must be written at the end, otherwise it will match to it first, affecting the matching of subsequent statements.
  • After each mode is processed, be sure to use the;;;⩔;&Come to an end. If none of these three are provided, an error will be reported.
  • ;;It means no longer matching down, it will end the wholecaseCommand execution. The effect is similar to that inselectExecute inbreakCommand.
  • ;&Means to continue executing the statement in the next pattern without checking whether the next pattern matches. Above"big lion")Mode used;&At the end, as you can see from the execution result, it will execute downward"fall through from big lion")Pattern.
  • ;;&Indicates to continue matching. If a matching pattern is found, the statement in the pattern will be executed. Above2 | 3 | 4)Mode used;;&At the end, when it matches to 2, it continues to match down, no match is found in the middle, until*)It’s a match. It’s done*)The statement in the pattern. When it’s 4, it’s down to$var)Mode, then$var)Used in the mode;;End execution, no further matching down.

In practical application, thecaseCommand andgetoptsCommand in combination,getoptsCommand gets the option parameter passed in when executing the script,caseThe command processes differently according to different option parameters. A simple example is as follows:

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

Can also putselectCommand andcaseCommand in combination,selectCommand to get the item selected by the user,caseThe command performs different processing according to different items selected by the user. No more examples here.