The syntax format of case is as follows:
Case VaR in
Exp-1) command body 1 ;;
Exp-2) command body 2 ;;
...
*) Command body ;;
Shell calculates the value of the variable VAR, and compares the result with the formula exp-1 and exp-2 until a matching formula is found. If a match is found, run the following command until a semicolon (;) is encountered.
Generally, the case statement is relatively small and occasionally used, so the record is as follows:
1> VaR can be a number or a string, just to distinguish between them in the following judgment formula exp.
2> the judgment formula exp can be a number or a string, or a formula such as $ (a + B) where A and B have been assigned a value on it, it can also be a regular expression.
3> * This is the command body that is executed only when all the judgment operators match. This item is optional.
4> A pair of Semicolons can be followed by the last statement of the command body, or written in the next blank line after the command body ends.
A Program is as follows:
param = $1
A = 11
B = 12
echo "parama =" $ Param
case $ Param in
$ (a + B ))) echo "No1"
param = "dog";
Hello) echo "NO2"
param = "cat";
10) echo "NO3"
param = "pig";
*) echo "error"
esac
echo "paramb =" $ Param
when this program is executed, I can take parameters 10 or 23 or "hello" to see the differences in the output Param. you can also set
case ...... esac Code is placed in (), and then executed separately to observe Param printing. when executing the code in (), shell will create a new sub-process, so the modification to Param in the sub-process does not affect the value of the variable in the parent process.