Case of shell syntax

Source: Internet
Author: User
Tags case statement

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.