3 Kinds of TEST commands:
Test EXPRESSION
[EXPRESSION]
[[EXPRESSION]]
Note: You must have a white space character before and after expression
Test type of Bash
Numerical test:
-eq: Is it equal
-ne: Whether or not to wait
-ge: is greater than or equal to
-le: is less than or equal to
-GT: Is it greater than
-LT: Is it less than
String test:
= or = =: is equal to
: Is it greater than
<: is less than
! =: is not equal to
=~: Whether the left string can be used in the right pattern matching [[]]
-Z "string": test whether the string is empty and Null is true
-N "string": test whether the string is non-empty, not empty is true
Note: The operands used for string comparisons should be enclosed in quotation marks.
File test:
Existence test:
-a FILE: Same as Next
-e File: Test for existence of files, existence is true, otherwise false
Presence and category testing:
-B File: Exists and is a block device file
-C file: exists and is a character device file
-D file: Exists and is a catalog file
-F file: exists and is a normal file
-H file or-L file: exists and is a symbolic link file
-P file: exists and is a named pipe file
-S file: exists and is a socket file
File permission test:
-R FILE: exists and is readable
-W FILE: exists and is writable
-X FILE: exists and is executable
File Special Permissions Test:
-G FILE: Exists and has Sgid permissions
-U FILE: Exists and has suid permissions
-K FILE: Exists and has sticky permissions
File size test:
-S FILE: exists and is not empty
Whether the file is open:
-T FD:FD indicates whether the file descriptor is open and related to a terminal
Other:
-N File: Whether the file has been modified since the last time it was read
-O File: Whether the current active user is a file owner
-G file: whether the current active user is a group of files
Binocular test:
File1-ef whether File2:file1 and FILE2 point to the same inode on the same device
File1-nt File2:file1 is new to FILE2
File1-ot file2:file1 whether old and FILE2
Combination condition test:
Logical operation:
Used in []:
Expression1-a expression2:2 conditions are true at the same time, the result is true
Expression1-o Expression2:2 One of the conditions is true, the result is true
! Expression: The result is true when the condition is false
[[]] or [] * * [] Use: NOTE: * * for && or | |
&&: Both the left and right conditions or commands are true at the same time, the result is true
|| : The result is true when one of the conditions or commands on either side is true
!: The result is true when the condition or command is False
Note: The use of parentheses in [] requires escaping that is \ (\), [[]] without escaping
Bash shell condition test