Introduction to Commands:
Condition Judgment:
If the user does not exist
Add user, give password and display add success;
otherwise
Show if no already in, no add;
Variable name:
1 , can contain only letters, numbers, and underscores, and cannot begin with numbers;
2 , should not be the same as the existing environment variables in the system;
3 , it is best to see the name and know righteousness;
Bash how to achieve the conditional judgment?
Condition Test Type:
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;
-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
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 first condition is true, the second condition must be judged;
logical OR : | |
4 . Command instance:
If the user User6 does not exist, the user is added User6
! ID USER6 && useradd user6
ID User6 | | Useradd User6
If the number of rows in the/etc/inittab file is greater than 100, a large file is displayed;
[' Wc-l/etc/inittab | cut-d '-f1 '-gt] && echo "Large file."
If the user exists, the user is already present, otherwise the user is added;
ID user1 && echo "user1 exists." | | Useradd user1
If the user does not exist, add; otherwise, show that it already exists;
! ID user1 && Useradd user1 | | echo "User1 exists."
If the user does not exist, add and give the password, otherwise, show that it already exists;
! ID user1 && useradd user1 && echo "user1" |passwd--stdin User1 | | echo "User1exists."
This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1863256
Linux conditional judgment: eq, NE, GT, lt, GE, le