case
Statements are used to execute different code blocks based on the value of an expression.
Basic structure
case expression in model1) Code block1 ;; model2) Code block2 ;; ... *) 默认Code block ;; esac
Detailed explanation
-
case expression in
:-
case
Start a keywordcase
Sentence. -
expression
is the value to match, which can be a variable or a fixed value. -
in
Keywords indicate the beginning of the pattern list.
-
-
model:
- Each pattern must be followed by a close bracket.
)
, mark the end of the mode. - The mode can be a single value or multiple values
|
Separate, indicating that multiple values can match the same code block.
- Each pattern must be followed by a close bracket.
-
Code block:
- The code block corresponding to each pattern is a series of commands that are executed when the match is successful.
- Code blocks
;;
End, indicating the end of the branch.
-
*)
:-
*)
is the default branch, used to handle all cases that do not match any previous pattern. - The default branch is also optional, but it is generally recommended to include default branches to handle unexpected situations.
-
-
esac
:-
esac
Keyword ends the wholecase
Sentence.
-
Example
Example 1: Basic usage
#!/bin/bash # Get user inputread -p "Please enter a number (1-7): " num # output the corresponding week according to the input numbercase $num in 1) echo "Monday" ;; 2) echo "Tuesday" ;; 3) echo "Wednesday" ;; 4) echo "Thursday" ;; 5) echo "Friday" ;; 6) echo "Saturday" ;; 7) echo "Sunday" ;; *) echo "Invalid input" ;; esac
Example 2: Multi-value Matching
#!/bin/bash # Get user inputread -p "Please enter a letter (a-z): " letter # Output the corresponding information according to the input letterscase $letter in a|e|i|o|u) echo "This is a vowel" ;; b|c|d|f|g|h|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z) echo "This is a consonant letter" ;; *) echo "Invalid input" ;; esac
Example 3: Range Matching
#!/bin/bash # Get user inputread -p "Please enter a number (1-100): " num # The corresponding range according to the input digital outputcase $num in [1-9]) echo "The numbers are in 1 arrive 9 between" ;; [1-9][0-9]) echo "The numbers are in 10 arrive 99 between" ;; 100) echo "The number is 100" ;; *) echo "Invalid input" ;; esac
Special usage
-
Wildcard:
- Wildcards can be used
*
and?
Perform fuzzy matching. -
*
Match any character of any length. -
?
Match a single arbitrary character.
- Wildcards can be used
-
Compound mode:
- Brackets can be used
()
Contains multiple modes to form a composite mode.
- Brackets can be used
Example 4: Wildcard Match
#!/bin/bash # Get user inputread -p "Please enter a file name: " filename # Output the corresponding information according to the entered file namecase $filename in *.txt) echo "This is a text file" ;; *.jpg|*.jpeg|*.png) echo "This is a picture file" ;; *) echo "Unknown type of file" ;; esac
Summarize
case
Statements are a powerful conditional control structure that is suitable for a variety of scenarios, especially when different operations need to be performed based on multiple possible values. By using pattern matching and default branches rationally, you can write concise and easy-to-maintain scripts.
This is the end of this article about the use of case in shell. For more related shell case content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!