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