Shell Programming---condition judgment

Source: Internet
Author: User

I. Condition judgment of shell programming

1. Integer test

A. Expressions for conditional tests

[Expression]

[[Expression]]

Test expression

B. Integer comparison

-EQ: Tests whether two integers are equal, such as $A-eq $B

-ne: Test whether the two integers are unequal, unequal, true, equal, false;

-GT: Tests whether one number is greater than the other, greater than, true, or false;

-LT: Tests whether one number is less than the other, less than, true; otherwise, false;

-ge: greater than or equal to

-le: Less than or equal to

C. The logical relationship between commands

Logic and: &&

When the first condition is false, the second condition is no longer judged and the final result is

When the first condition is true, the second condition must be judged

Logical OR: | | when the first condition is true, the second condition is no longer judged

Logical non: Take counter

Add User if existing

# ID Username | | Useradd username

If the user exists, the user is already present, otherwise the user is added

# ID user1 && echo "user1 exists." | | Useradd user1

Add if the user does not exist; otherwise, it shows that it already exists

# ! ID user1 && Useradd user1 | | echo "User1 exists."

If the user does not exist, add and give the password; otherwise, it shows that it already exists

# ! ID user1 && useradd user1 && echo "user1" | passwd--stdin User1 | | echo "User1 exists."

2. Condition judgment, control structure

Single Branch if statement

if judgment condition; Then

Statement1

Statement2

...

Fi


If statements for dual branches

if judgment condition; Then

Statement1

Statement2

...

Else

Statement3

Statement4

...

Fi

Multi-Branch if statement

if judgment condition 1; Then

Statement1

...

Elif judgment Condition 2; Then

Statement2

...

Elif judgment Condition 3; Then

Statement3

...

Else

Statement4

...

Fi



This article is from "Luo Chen's blog" blog, please be sure to keep this source http://luochen2015.blog.51cto.com/9772274/1636707

Shell Programming---condition judgment

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.