case - string testing structure.
case that perform an if, else if, ..., else test sequence on
a provided string.
case uses the file-name wild-card metacharacters to describe a
match template to compare a provide string against. If matched, a block
of specified actions are then executed.
case general structure
case "$var" in
str_template-1 )
action_cmd[s]
;;
str_template-2 )
action_cmd[s]
;;
esac
case - keyword starting case structure.
"$var" - variable containing string to be tested.
Usually quoted so a null string does not crash case statement.
in - keyword closes the line specifying the target string.
str_template-1 - string pattern to match against.
Match is exact.
Wild cards and alternative matches used to create a more general
match pattern.
) - right parenthesis indicates close of a match statement.
action_cmd[s] - action[s] to take
if variable contents matches string pattern.
This can be a whole block of bash code including other case statements
Or even invoke other programs.
;; - double semi-colon, indicates end of particular action block.
esac - keyword that terminates the case structure.
case features and properties
- Multiple str_template / action blocks may be specified.
- Each block is exclusive.
- Each block can contain any amount of code include other case statements.
- Once a block is matched/executed, the case exits out the bottom.
- Place specific tests first.
Match value starting with B (capitalized)
action 1
Match value starting with any alpha character
action 2
Any values starting with B will be matched first,.
******
Match value starting with any alpha character
action 2
Match value starting with B (capitalized)
action 1
In this case, the second match will never occur.
Template match patterns and wild-cards
The str_template supports wild-cards modeled after the filename wild-cards.
These can be combined in any order or repetition. Just remember 1st match is
only match, so work from specific to general.