Shell script conditional statement writing

Source: Internet
Author: User

Step 1: Master several if statement branches

The following is the most complete if format. You can delete it as needed in actual use.

If condition; then

// Action

Elif conditon: then

// Action

... Several intermediate Elif

Else

// Action

Then

// Action

Fi

Here are some notes

1. action can be blank

2. If contion; then can also be written

If condion

Then

Step 2: master the conditional writing

Let's take a look at the condition syntax.

In fact, the general conditions are mainly divided into two parts: string and value. A special one in UNIX systems is File Permission detection. You don't have to worry about these comparisons. You can use special commands.

Comparison Commands include:

1. Test

2. expr

Both of them can compare strings and numbers. However, test can also compare certain attributes of files, such as X, W, R, and U.

Next, we will briefly introduce test and expr.

Format: Test condtion or [condtion] (Note that there must be spaces on both sides of square brackets)

Returned value: 0 correct 1 returned error

Usage:

1. Test file attributes: [-X file]

2. String test: = ,! =,-Z,-,-N respectively indicate equal, unequal, empty string, non-empty string

3. Numerical Test:-GT,-lt,-Ge,-Le,-EQ, and-ne respectively indicate greater than, less than, greater than or equal to, less than or equal to, and not equal

Expr is mainly used for integer calculation. It can also be used for numerical comparison and string comparison. The most amazing thing is that it can also be used for pattern matching. [However, I can definitely use awk, sed, or grep instead]

+ ,-,*,/

Note: When Using *, you must add the Escape Character \

Refer:

Http://techbbs.zol.com.cn/1/60_1601.html ###

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.