Bash comparison Classification
Test method:
[Expression]
[[Expression]
Test expression
Common condition tests in bash:
1. Integer Test
2. File Test
3. String comparison test
Integer test:
-Eq: test whether two integers are equal. For example, $ A-eq $ B. If the values of A and B are equal, 0 is returned. Otherwise, A non-0 value is returned.
-Nq: test whether two integers are unequal. equal to false, unequal to true
-Gt: test whether one number is greater than the other. If the value is greater than true, otherwise the value is false.
-Lt: test whether a number is smaller than zero. If the value is smaller than true, the value is false.
-Ge: test whether a number is greater than or equal to the other number. If it is greater than or equal to true, otherwise it is false.
-Le: test whether a number is less than or equal to the other number. If it is less than or equal to true, otherwise it is false.
INT1 = 34
INT2 = 44
[$ INT1-eq $ INT2]
[[$ INT1-eq $ INT2]
Test $ INT1-eq $ INT2
If [grep "^ USERNAME \>"/etc/passwd]; then # This method is incorrect.
If grep "^ USERNAME \>"/etc/passwd; then # is written in this way.
File test:
-E FILENAME: whether the file exists
-F FILENAME: test whether the file is a common file.
-D FILENAME: test whether the specified path is a directory.
-R FILENAME: test whether the current user has the read permission on the specified file.
-W FILENAME: test whether the current user has the write permission on the specified file.
-X FILENAME: test whether the current user has the execution permission on the specified file.
String comparison test:
= OR =: equality test, equal to true, not equal to false [$ A = $ B], equal signs must have spaces at both ends
! =: Whether the test is unequal. It is not a Wei Zhen, and the test is false.
>:
<:
-Z string: test whether the specified string is null. If it is null, it is true. If it is not null, it is false.
-N string: test whether the specified string is null. If it is not null, it is true. If it is null, it is false.