1. Conditional Test
Use the test command for files, strings, and numbers
Use the expr command for numbers and strings
Expr command to test and execute numerical output
1. Test File status
Test generally has two formats:
Test Condition
[Conditon]
-D directory
-F: regular file
-L symbolic connection
-R readable
-S file length is greater than 0, not empty
-W writable
-The U file has SUID settings
-X executable
Use two methods to test whether scores.txt can be written and use the final exit status to test whether scores.txt is successful.
$ [-W scores.txt]
$ Echo $?
0
$ Test-W scores.txt
$ Echo $?
0
Both statuses return 0 to the writable scores.txt file.
2. Use logical operators during testing
-A logic and
-O logic or
! Logic No
Test whether two files are readable.
[-W file1.txt-a-W file2.txt]
Echo $?
3 string Testing
Five formats for string Testing
Test "string"
Test string_operator "string"
Test "string" string_operator "string"
[String_operator string]
[String string_operator string]
Here, string_operator can be:
=
! =
-Z empty string
-N non-empty string
Test whether the environment variable editor is empty
$ [-Z $ Editor]
$ Echo $?
1
Non-null. Is the value Vi?
$ [$ Editor = "Vi"]
$ Echo $?
0
4. Test Value
"Number" numeric_operator "Number"
Numeric_operator can be:
-Equal EQ values
-Ne value range
-GT: the first number is greater than the second number.
-Lt. The first number is smaller than the second number.
-The first number of Le is greater than or equal to the second number.
-Ge: the first number is greater than or equal to the second number.
$ ["990"-Le "995"-a "123"-GT "33"]
$ Echo $?
0
5 expr usage
The expr command is generally used as an integer, but can also be used as a string.
Expr argument operator argument
Expr is also a manual command line counter
$ Expr 10 + 10
20
$ Expr 30/3
10
Use \
$ Expr 30 \ * 3
90
Incremental count
Expr is used for incremental computing in a loop. First, the loop is initialized to 0, and then the loop value is added to 1. The use of anti-quotation marks is an alternative command. The most basic one is to accept the output from the (expr) command and put it into the cyclic variable.
$ Loop = 0
$ Loop = 'expr $ loop + 1'
numerical test