Integer test: Do numeric size and equality comparisons
$A-lt $B: Indicates whether it is less than
$A-gt $B: Indicates whether it is greater than
$A-le $B: is less than or equal to
$A-gt $B: is greater than or equal to
$A-eq $B: Indicates whether it is equal to
$A-ne $B: Indicates whether it is not equal to
Example: $A less than $b echo $? For "0" to indicate success
[[email protected] ~]# a=10[[email protected] ~]# b=20[[email protected] ~]# [$A-lt $B][[email protected] ~]# echo $?0
Example: $A greater than $b echo $? For "0" to indicate success
[[email protected] ~]# a=20[[email protected] ~]# b=10[[email protected] ~]# [$A-gt $B][[email protected] ~]# echo $?0
Example: $A greater than or equal to $b echo $? For "0" to indicate success
[[email protected] ~]# a=20[[email protected] ~]# b=10[[email protected] ~]# [$A-ge $B][[email protected] ~]# echo $?0
Example: $A less than or equal to $b echo $? For "0" to indicate success
[[email protected] ~]# a=10[[email protected] ~]# b=10[[email protected] ~]# [$A-le $B][[email protected] ~]# echo $?0
Example: $A equals $b echo $? = "0" for success
[[email protected] ~]# a=10[[email protected] ~]# b=10[[email protected] ~]# [$A-eq $B][[email protected] ~]# echo $?0
Example: $A not equal to $b echo $? For "0" to indicate success
[[email protected] ~]# a=10[[email protected] ~]# b=20[[email protected] ~]# [$A-ne $B][[email protected] ~]# echo $?0
String test: The larger the ASCII value, the greater the value of the character comparison
"$A" > "$B": Indicates whether it is greater than
"$A" < "$B": Indicates whether it is less than
"$A" = = "$B": Indicates whether it is equal to
"$A"! = "$B": Indicates whether it is not equal to
-Z "$A": Indicates whether it is empty
-N "$A": Indicates whether it is not empty
Note: When you do a string comparison, the higher the value of the letter, the more the letters are compared sequentially.
Example: $A less than $B echo $? For "0" indicates success
[[email protected] ~]# a=abc[[email protected] ~]# b=abd[[email protected] ~]# [["$A" < "$B"]][[email protected] ~]# Echo $?0
Example: $B greater than $A echo $? = "0" for success
[[email protected] ~]# a=abc[[email protected] ~]# b=abd[[email protected] ~]# [["$B" > "$A"]][[email protected] ~]# Echo $?0
Example: $A equals $B echo $? Indicates success for "0"
[[email protected] ~]# a=abc[[email protected] ~]# b=abc[[email protected] ~]# [["$A" = = "$B"]][[email protected] ~]# EC Ho $?0
Example: $A not equal to $B echo $? For "0" indicates success
[[email protected] ~]# a=abc[[email protected] ~]# b=ab[[email protected] ~]# [["$A"! = "$B"]][[email protected] ~]# Ech o $?0
Example: Judging if $ A is empty, echo $? = "0" for success
[Email protected] ~]# a=
[Email protected] ~]# [-Z $A]
[[email protected] ~]# echo $?
0
[Email protected] ~]# echo $A
[Email protected] ~]#
Example: To determine if a $ A is not empty, echo $? For "0" indicates success
[Email protected] ~]# a=abc[[email protected] ~]# [-N $A][[email protected] ~]# echo $?0[[email protected] ~]# echo $AA Bc
File testing: Testing the existence of files and properties
-E or-a $file: exists, exists as "true", "otherwise false"
-F $file: File exists and is normal file
-D $file: Whether the file exists and is a directory
-H $file: exists and is a symbolic link file
-B $file: exists and is a block device file
-C $file: exists and is a character device file
-S $file: exists and is a socket file
-P $file: exists and is a pipe file
-R $file: Whether the current user has read access to the file
-W $file: Whether the current user has write access to the file
-X $file: Whether the current user has execute permissions on the file
-U $file: Whether the file has suid permissions
-G $file: Whether the file has Sgid permissions
-K $file: Whether the file has sticky permissions
-O $file: Whether the current user is the owner of the specified file
-G $file: Whether the current user is a generic group of the specified file
Example: Determine if a file exists, exists as true, or false
[Email protected] ~]# [-e/root/file][[email protected] ~]# echo $?0[[email protected] ~]# ls/root/file-l-rw-r--r--. 1 root root 0 Sep 13:12/root/file[[email protected] ~]#
Example: Determine if a file exists and is a normal file, exists as "true", "otherwise false"
[Email protected] ~]# [-f/root/file][[email protected] ~]# echo $?0[[email protected] ~]# ls/root/file-l-rw-r--r--. 1 root root 0 Sep 13:12/root/file
Example: Determine if a directory exists and is a normal file, exists as "true", "otherwise false"
[Email protected] ~]# [-d/root/][[email protected] ~]# echo $?0[[email protected] ~]# ll-d/root/dr-xr-x---. Root root 4096 Sep 13:12/root/
Example: Determining whether the current user has read access to a file
[Email protected] ~]# [-r/root/file][[email protected] ~]# echo $?0[[email protected] ~]# ll/root/file-rw-r--r--. 1 root root 0 Sep 13:12/root/file
Example: Determining whether the current user has write permission on a file
[Email protected] ~]# [-w/root/file][[email protected] ~]# echo $?0[[email protected] ~]# ll/root/file-rw-r--r--. 1 root root 0 Sep 13:12/root/file
Example: Determining whether the current user has execute permissions on a file
[[email protected] ~]# chmod +x/root/file[[email protected] ~]# [-x/root/file][[email protected] ~]# echo $?0[[email p Rotected] ~]# ll/root/file-rwxr-xr-x. 1 root root 0 Sep 13:12/root/file
Example: Determine whether the current user is the owner of the specified file
[Email protected] ~]# [-o/root/file][[email protected] ~]# echo $?0[[email protected] ~]# ll/root/file-rw-r--r--. 1 root root 0 Sep 13:12/root/file
Example: Determining whether the current user is a group of the specified file
[Email protected] ~]# [-g/root/file][[email protected] ~]# echo $?0[[email protected] ~]# ll/root/file-rw-r--r--. 1 root root 0 Sep 13:12/root/file
Example: Determine if a file has suid permissions
[Email protected] ~]# ll/root/file-rw-r--r--. 1 root root 0 Sep 13:12/root/file[[email protected] ~]# chmod 4644/root/file[[email protected] ~]# [-u/root/file] [ [Email protected] ~]# echo $?0[[email protected] ~]# ll/root/file-rwsr--r--. 1 root root 0 Sep 13:12/root/file
Example: Determine if a file has Sgid permissions
[Email protected] ~]# chmod 2644/root/file[[email protected] ~]# [-g/root/file][[email protected] ~]# echo $?0[[email Protected] ~]# LL/ROOT/FILE-RW-R-SR--. 1 root root 0 Sep 13:12/root/file
Example: Determine if a file has sticky permissions
[Email protected] ~]# chmod 1644/root/file[[email protected] ~]# [-k/root/file][[email protected] ~]# echo $?0[[email Protected] ~]# ll/root/file-rw-r--r-t. 1 root root 0 Sep 13:12/root/file
Binocular operator:
$file 1-nt $file Whether 2:file1 is new to File2, File1 's last modified timestamp is later than File2 's
$file 1-ot $file Whether 2:file1 is older than File2, File1 's last modified timestamp is earlier than File2 's
$file 1-ef $file whether the 2:file1 and File2 point to the same inode; test whether they are a hard link to the same file
Combination test conditions: logical operations, expression combinations, and command combinations
Logical operation:
With: Multiple conditions to meet simultaneously
Or: Multiple conditions satisfy one
Non: reverse the specified condition
Expression combinations:
With: Judging the file exists and is an ordinary file
[Email protected] ~]# [-e/root/file-a-f/root/file][[email protected] ~]# echo $?0
Or: Determine if the file is a normal file or a directory file
[Email protected] ~]# [-d/root/file-o-f/root/file][[email protected] ~]# echo $?0
Non: Judgment file is not a directory file
[[Email protected] ~]# [! -d/root/file][[email protected] ~]# echo $?0
Command combination:
With: COMMAND1 && COMMAND2 COMMAND1 executed after successful execution COMMAND2
[email protected] ~]# Touch file && ll file-rw-r--r-t. 1 root root 0 Sep 14:36 file
Or: COMMAND1 | | COMMAND2 COMMAND1 execution is unsuccessful after executing COMMAND2
[[email protected] ~]# ID Alice | | Useradd aliceid:alice:No such user[[email protected] ~]# ID aliceuid=511 (Alice) gid=511 (Alice) groups=511 (Alice)
Non -:! command to reverse the order
[Email protected] ~]# [-f/root/file][[email protected] ~]# echo $?0[[email protected] ~]# [!-f/root/file][[email P Rotected] ~]# Echo $?1
Short-circuit operator:
&&:command1 && COMMAND2 COMMAND1 execute after successful execution COMMAND2
| |: COMMAND1 | | COMMAND2 COMMAND1 execution is unsuccessful after executing COMMAND2
! Command: Reverse the order
BASH Programming: Conditional testing