Fun Bash Script: test statement
1st articles
Test
Test is the meaning of testing. It is often used as a condition in flow control statements. The following is an introduction.
Test integer
The test of the integer is the comparison of the size relationship. Unlike other languages, <,> is not used in Bash to compare the values greater than or equal to the number.
Assume there are two integer variables, a and B. Then compare whether a is greater than B to writeTest $ a-gt $ B , Returns the true value 1 if the condition is met. In actual test on the terminal, you can write the test statement execution result in this way.
test $a -gt $ $b && echo “Yes”
If the condition is true, print Yes. If the condition is not true, do not print.
All link comparison options are:
Option |
Description |
Full name |
Eq |
Equal |
Equal |
Gt |
Greater |
Greater |
Lt |
Less |
Less |
Ne |
Not equal |
Not equal |
Ge |
Greater than or equal |
Greater or equal |
Le |
Less than or equal |
Less or equal |
Bash scripts, or various commands in Shell terminals, have rich options. Therefore, I suggest memorizing the full English name and understanding its meaning when memorizing it, this will reduce the difficulty of memory. Test string
A string test is nothing more than a test that checks whether two strings are equal and whether a string is null.
Assume that str1 and str2 are variables that hold two strings (directly test the two strings instead of the string variables without adding $, which is understandable ). Usage:
Usage |
Description |
Test $ str1 = $ str2 |
Test whether the values are equal. If the values are equal, 1 is returned. |
Test $ str1! = $ Str2 |
Whether the test is not equal. If not, 1 is returned. |
Test $ str1 \ <$ str2 |
If the Lexicographic Order of str1 is after str2, 1 is returned. |
Test $ str1 \> $ str2 |
If the Lexicographic Order of str1 is before str2, 1 is returned. |
Test $ str1 |
If not empty, 1 is returned. |
Test-n $ str1 |
If not empty, 1 is returned. |
Test-z $ str1 |
If it is an empty string, 1 is returned. |
The two comparisons of lexicographic orders are actually greater>And Yu no.<. The two symbols in bash have redirection meaning, so the backslash is used here.\Escape.
Option-n is short for nonzero. Option-z is short for zero. This is a good memory.
Test File
A large number of options are required for testing files. Here I only write one common option.Man test
For a single file
Option |
Description |
D |
Is it a directory? |
F |
Is it a common file? |
X |
Execution permission? |
R |
Read Permission? |
W |
Write and read permissions? |
E |
Exist? |
S |
Whether the file size is greater than 0 |
C |
Whether it is a character Device File |
B |
Whether it is a block Device File |
If the preceding conditions are true, 1 is returned. For example:
test -f hello.ctest -d /home...
For two files
Usage |
Description |
Test file1-nt file2 |
Test whether the modification time of file1 is later than that of file2 new) |
Test file1-ot file2 |
Test whether the modification time of file1 is earlier than that of file2 old) |
Test file1-ef file2 |
Test whether the two devices are the same and have the same inode number. |
Similarly, if the condition is true, true value 1 is returned. Note that file1 and file2 are strings of file names.
# Test a. c-nt B. c # Or a = a. cb = B. ctest $ a-nt $ B
No more details. Logical operation
Logical operations are and or not.
-A logic and-o logic or! Non-logical
Usage example:
Test $ a-lt $ B-a $ a-gt $ ctest $ a-lt $ B-o $ a-gt $ ctest! -D sleep. sh & echo Yes # If sleep. sh is not a directory, print Yes
The full version is as follows:
test $a -lt $b -a test $a -gt $ctest $a -lt $b -o test $a -gt $c
Simplified Version test
After reading this part, you may be surprised, why don't you tell me earlier. Haha.
In fact, all the above test commands can be replaced with square brackets. For example
Test-f hello. c
Can be changed
[-F hello. c]
It's very convenient. Note that there is a square brackets and an expression before and after each
SpaceInterval. Do not discard it. Actually [space-f hello. c space]
Other notes are the logical expressions in square brackets, such
Test $ a-lt $ B-a $ a-gt $ c
Can be converted
[$ A-lt $ B-a $ a-lt $ c]
Or separate them as two statements.
[$ A-lt $ B-a] & [$ a-lt $ c]
This is nothing special, not exclusive to square brackets. When the current statement is true, run the following statement. All Bash statements can be connected with & |.