Shell script Learning (2): conditional statements and cyclic statements

Source: Internet
Author: User
Shell script Learning (2): conditional statements and loop statements 1. conditional statements

First, when it comes to conditional statements, we have to first talk about the judgment of the conditional expressions. What types of conditional expressions are there in shell? There are three types:

1. String judgment

= Equals to true.

! = Not equal to true.

The-Z string length is false.

-If the length of the N string is not pseudo, it is true.

2. Integer judgment

-If EQ is equal to, it is true.

-If the ne value is not equal to the ne value, it is true.

-A value greater than-GT is true.

-If Ge is greater than or equal to, it is true.

-If it is less than, it is true.

-If the value of-Le is less than or equal to, it is true.

3. File judgment

-E: the file name is true if the file exists.

-R: The file name is true if the file exists and is readable.

-W: The file name is true if the file exists and can be written.

-X: the file name is true if the file exists and can be executed.

-S file name is true if the file exists and contains at least one character.

-D. The file name is true if the file exists and is a directory.

-F: The file name is true if the file exists and is a normal file.

-C: The file name is true if the file exists and is a special character file.

-B: The file name is true if the file exists and is a special file.

In addition, Linux also provides (!) , Or (-O), non-(-a) logical operators, used to connect test conditions, the priority is :! Highest, followed by-A and-o.

The judgment of the conditional expression is described above. Let's talk about the conditional expressions and their usage.

1> If Condition Statement

The two condition branches in the shell program are implemented through the IF Condition Statement, for example:

If [conditional expression]; then

Execute when the condition is true

Fi

Or

If [conditional expression]; then

Execute when the condition is true

Else

Execute when the condition is false

Fi

2> case multi-branch statement

The multi-path branch statement case is used for multi-condition testing. For example:

Case $1 in

File 1)

Echo "file 1 ......."

;;

File 2)

Echo "file 2 ......."

;;

*)

Echo "select file1 or file2"

;;

Esac

Ii. Loop Statement 1> for loop, for example:

For I in 1 2 3 4

Do

Echo $ I

Done

2> while loop, for example:

While condition expression

Do

Execution statement

Done

3> until loop, similar to while, for example:

Until conditional expression

Do

Execution statement

Done

Linux Shell also has continue and break in loop control statements, which are used in the same way as C.

Now, the conditional control in Linux shell scripts is similar to loop control.

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.