The Shell Case statement is a multiple-selection statement. You can use a case statement to match a value with a pattern, and if the match succeeds, execute the matching command. The case statement is formatted as follows:
Copy Code code as follows:
Case value in
Mode 1)
Command1
Command2
...
CommandN
;;
Mode 2)
Command1
Command2
...
CommandN
;;
Esac
The case works as shown above. The value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be a variable or a constant. When a match finds a value that conforms to a pattern, all commands begin to execute until;
The value will detect each pattern that matches. Once the pattern matches, the corresponding command after the match pattern is executed and no more mode continues. If there is no matching pattern, use the asterisk * to capture the value, and then execute the following command.
The following script prompts you to enter 1 to 4, matching each pattern:
Copy Code code as follows:
Echo ' Input a number between 1 to 4 '
Echo ' Your number is:\c '
Read Anum
Case $aNum in
1) echo ' you select 1 '
;;
2) echo ' You select 2 '
;;
3) echo ' you select 3 '
;;
4) echo ' You select 4 '
;;
*) echo ' You don't select a number between 1 to 4 '
;;
Esac
Input different content, there will be different results, such as:
Input a number between 1 to 4
Your number Is:3
you select 3