Case statements are used to implement multiple if. else, but the usage is somewhat different, people who have learned other languages know that the case to match the variables, if there is a matching successful words to execute the corresponding statement. The case statement in Shell programming is also this meaning, cases will match the string, starting from the first mode, if a pattern has been matched successfully, the other mode will not be matched, the following look at the use of the code.
#!/bin/sh
echo "Please yes or no"
read input
#case语句的基本用法, remember that each match has a double semicolon behind it, representing the end of this pattern and the beginning of the next # pattern, Do not add double quotes when matching wildcard characters "
$input" in
Yes) echo "yes";;
y*) echo "y*";;
Y) echo "y";;
NO) echo "no";;
n) echo "n";;
*) echo "default";;
Esac
#合并匹配模式, case matching can only match one, want to match multiple results using the following way case
"$input" in
y* | y*) echo "yes";;
n | N) echo "no";;
*) echo "default";;
Esac
#一种更屌的匹配 Case
"$input" in
[Yy][ee][ss] | [YY]) echo "yes";;
[NN] | [NN] [OO]) echo "no";;
*) echo "default"
echo "End"
Esac
exit 0