Linux Test Statements
Testing with the test command, returning 0 means that the test was successful and returning 1 indicates a failure.
1. File Status Test
File status test refers to the permissions of the file, there is no, attributes, types and other content to judge.
File Test parameter list
-d test file is a catalog file |
-S test file is not empty |
-F Test file is a regular file |
-W test file is writable |
-L Test if the file is a symbolic connection |
-U Test whether the file has suid bit settings |
-X Test whether the file can be executed |
-R Test file is readable |
2. Numerical test
The numerical test is to compare the size relationship of two values
1) test grammar with the test command: Test first operand numeric comparer second operand
2) use [] instead of the test command syntax:[ first operand numeric comparer second operand ]
Note:[ must have a space ] before must also have a space
Numeric comparison characters
-eq equals |
-ne Not equal to |
-ge greater than or equal to |
-GT greater than |
-le less than or equal to |
-lt less than |
3. String test
A string test is to compare two strings for equality, or to determine whether a string is empty
The method is similar to a numeric test by comparing the following characters:
= tests whether two strings are equal |
! = Tests whether two strings are not equal |
-Z Test string is empty |
-N test string is non-null |
4. Logic Test
-A: logical and, when both sides of the operator are true, the result is true, otherwise the result is false.
-O: logical OR, at least one of the operators is true, the result is true, otherwise false.
! : Logical No, only if the condition is false - Returns the result as true
Linux Test Statement (tests)