Bash Script Programming second, conditional judgment
Condition Judgment:
If the user does not exist
Add user, give password and display add success;
Otherwise
Show that the user is already present, not added
How do I make conditional judgments in bash?
Integer test
Character Test
File test
Expressions for Conditional tests:
[Expression]
[[Expression]]
Test expression
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;
-QT: Tests whether one number is greater than the other, greater than true;
-LT: Tests if one number is less than the other; less than true; otherwise, false;
-ge: greater than or equal to
-le: Less than or equal to
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 already there;
When the second condition is true, the second condition must be judged;
logical OR: | |
If the user user2 does not exist, the user is added User2
! ID user2 && useradd user2
ID User2 | | Useradd User2
variable name:${varname}, parentheses can sometimes be omitted
1, can only contain sub-mother, number and underscore, and can not start with a number;
2, should not be in the system with the existing system variables duplicate name;
3, it is best to see the name of the meaning;
If the user User1 exists, it shows that the user already exists; otherwise, it is added;
ID user1 && echo "user1 exists." | | Useradd user1
If the user User1 does not exist, add it, otherwise, show that it already exists;
! ID user1 && Useradd user1 | | echo "User1 exists."
If user User1 does not exist, add and give password; otherwise, show that it already exists;
! ID user1 && useradd user1 && echo "user1" | passwd--stdin User1 | | echo "User1 exists."
conditional judgment, control structure :
Single Branch if statement
if judgment condition; Then
Statement1
Statement2
...
Fi
Dual-Branch If statement:
if judgment condition; Then
Statement1
Statement2
Else
Statement3
Statement4
Fi
This article is from the "11913800" blog, please be sure to keep this source http://11923800.blog.51cto.com/11913800/1844019
Linux Learning Note--bash script programming the second, condition judgment