Condition test:
To determine whether a demand is satisfied, it needs to be realized by testing mechanism;
How to write a test expression to implement the required tests:
(1) Execute the command, and use the command status return value to judge;
$?: Command Status return value
0: Success
1-255: Failure
(2) test expression
Test EXPRESSION
[EXPRESSION]
[[EXPRESSION]]
Note: You must have a white-space character at both ends of expression, or a syntax error;
Example:
[[email protected] tmp]# Touch A[[email protected] tmp]# [-f a][[email protected] tmp]# echo $?0[[email protected] tmp]# [-D A] [[email protected] tmp]# echo $?1[[email protected] tmp]# test-f a[[email protected] tmp]# echo $?0[[email protected] tmp ]# [[-D $?]] [Email protected] tmp]# echo $?1
Test type of bash:
Numerical test
String test
File test
Numerical test: Numerical comparison
-eq: is equal to; [$num 1-eq $num 2]
-ne: is not equal to;
-GT: Is it greater than;
-ge: is greater than or equal to;
-LT: Is it less than;
-le: is less than or equal to;
String test:
= =: whether equal;
: Is it greater than;
<: Is it less than;
! =: Not equal;
=~: Whether the left string can be matched by the pattern on the right;
-Z "STRING": Determine whether the specified string is empty, empty is true, and not empty is false;
-N "string": Determines whether the specified string is not empty, is true, empty is false;
Attention:
(1) string to be quoted;
(2) to use [[]];
file Test :
Presence Testing
-A FILE
-E FILE
The existence of the file test, the existence is true, otherwise it is false;
Existence and type testing
-B File: Whether it exists and is a block device file;
-C File: Whether it exists and is a character device file;
-D file: Exists and is a directory 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 to the current user;
-W FILE: exists and is writable to the current user;
-X FILE: Exists and is executable to the current user;
Special permission tests:
-U FILE: Exists and has suid permissions;
-G FILE: Exists and has sgid permissions;
-K FILE: Exists and has sticky permissions;
Whether the file has content:
-S FILE: whether there is content;
Time stamp:
-N File: Whether the file has been modified since the last read operation;
Dependency testing:
-O File: Whether the current user is the owner of the file;
-G file: Whether the current user belongs to the genus Group of the file;
Binocular test:
File1-ef File2:file1 and FILE2 are hard links to the same inode as the same file system;
Whether File1-nt File2:file1 is new to FILE2;
File1-ot File2:file1 is older than FILE2;
Combination Test conditions:
Logical operation:
The first way:
COMMAND1 && COMMAND2
COMMAND1 | | COMMAND2
! COMMAND
[-o file] && [-R File]
The second way:
Expression1-a EXPRESSION2
Expression1-o EXPRESSION2
! EXPRESSION
[-O file-a-x FILE]
Exercise: Save the current host name to the hostname variable; If the hostname is empty or localhost.localdomain, set it to WLM;
[Email protected] tmp]# hostname=$ (hostName) [[email protected] tmp]# Hostnamelocalhost[[email protected] tmp]# echo $ HostName Localhost[[email protected] tmp]# [-Z "$hostName"-o "$hostName" = = "localhost"-o "$hostName" = = "Localhost.loc Aldomain "] && hostname wlm[[email protected] tmp]# echo $?0[[email protected] tmp]# HOSTNAMEWLM
This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1863926
Condition testing and judgment in the shell