Test commands for shell scripts
Conditional test command:
Test command = [command, built-in bash command;
Format:
Test [expression]
[Expression]
[[: Bash keyword; generally used only in certain structures or formats;
[[Expression]
Note: This type of conditional test command generally does not have an execution structure and only returns the execution status. If its execution status returns 0, it indicates true; if its execution status returns 1, false;
The conditional test command has three types of conditional test expressions:
1. Value test: binocular operation:
Test OPERATOR:
-Eq: test whether two values are equal. equal to true, not equal to false;
-Ne: test whether two values are unequal. They are not equal to true or false;
-Lt: smaller than, test whether the value on the left is smaller than the value on the right. If the value is less than true, it is not less than false;
-Gt: greater than, to test whether the value on the left is greater than the value on the right; greater than true, not greater than false;
-Le: If the value is less than or equal to, check whether the value on the left is less than or equal to the value on the right. If the value is less than or equal to true, the value greater than or equal to false;
-Ge: If the value is greater than or equal to, whether the value on the left is greater than or equal to the value on the right. If the value is greater than or equal to true, the value smaller than or equal to false;
Note: bash only supports certificates and does not support floating-point numbers;
[Root @ localhost ~] # Id wangcai
Uid = 5003 (wangcai) gid = 8089 (xiaoming) group = 8089 (xiaoming), 5002 (qiuxiang)
[Root @ localhost ~] # Test $ (id-u wangcai)-ge 1000 & echo "Common User" | echo "System User"
Common User
[Root @ localhost ~] # [$ (Id-u wangcai)-ge 1000] & echo "Common User" | echo "System User"
Common User
2. String test:
Binocular test:
==|=: Test whether the two strings are the same strings. If the strings are the same as the true strings, the differences are false strings;
! =: Test whether two strings are different strings. If the two strings are true, the same strings are false;
>: Test whether the ASCII code corresponding to the string on the left is greater than the ASCII code corresponding to the string on the right. If it is greater than true, it is less than false;
<: Test whether the ASCII code corresponding to the string on the left is smaller than the ASCII code corresponding to the string on the right. If the ASCII code is less than true, it is greater than false;
STRING = ~ PATTERN: to test whether the string on the Left can be matched by the PATTERN on the right. If the string on the right is true, it cannot be matched with false;
Note:>, <, = ~ These three types of test operators can only be used in [[expression] Test statements.
[Root @ localhost ~] #[[100 = 100] & echo "same number" | echo "different number"
Same number
Single-camera test:
-Z 'string': test whether the STRING is an empty STRING. If the empty STRING is true, the Null STRING is false;
-N 'string': test whether the STRING is a non-empty STRING. If it is not null, it is true, and the empty STRING is false;
Note:
1) Generally, strings must be referenced by quotation marks. Single or double quotation marks are allowed. Only single or double quotation marks can be selected under certain conditions;
2) [] and [[] In some cases, they have different meanings and should be treated differently;
3) If [] or [[] is used for condition determination, blank characters are left at both ends of the expression to distinguish between commands and parameters;
4) all binary test operators and parameters must also have a blank space;
File test:
Single-camera test:
-A |-e FILE: whether the test FILE exists. If it is true or not, it is false;
-B FILE: whether the test FILE exists and is a block device FILE. If the block device is true, the test FILE is false;
-C FILE: whether the test FILE exists and is a character device FILE. If it is a character device that is true, it is false;
-D FILE: test whether the FILE exists and is a directory FILE. If the directory FILE is true, the FILE is false;
-F FILE: test whether the FILE exists and is a common FILE. If the existing FILE is true, the FILE is false;
-H |-l FILE: test whether the FILE exists and is a symbolic link FILE. If the symbolic link FILE is true, the FILE is false;
-S FILE: whether the test FILE exists and its size is greater than 0. If its size is greater than 0, it is true. Otherwise, it is false;
-S file: test whether the FILE exists and is a socket FILE. If it is a socket FILE, the FILE is true. Otherwise, the FILE is false;
-T FD: test whether the file descriptor is opened on the terminal. Otherwise, the file descriptor is false;
File Access permission test:
-R FILE: test whether the FILE exists and whether the current user is granted the read permission. If yes, the authorization is true. Otherwise, the FILE is false;
-W FILE: test whether the FILE exists and whether the current user is granted the write permission. If the permission exists and the authorization is true, the FILE is false;
-X FILE: test whether the FILE exists and whether the current user is granted the execution permission. If yes, the authorization is true. Otherwise, the authorization is false;
Test the special permission identification of a file:
-U FILE: whether the test FILE exists and SUID is set, and the authorization is true. Otherwise, the test FILE is false;
-G FILE: whether the test FILE exists and has a SGID set, and the authorization is true. Otherwise, the test FILE is false;
-K FILE: test whether the FILE exists and STICKY is set, and the authorization is true. Otherwise, the FILE is false;
File Ownership test:
-O file: test whether the FILE exists and the owner is the current valid user. If the owner is the current valid user, the FILE is false;
-G file: whether the test FILE exists and its Group is the group of the current valid user. If the group exists and the group is the current valid user, the group is true. Otherwise, the group is false;
Binocular test:
FILE1-ef FILE2: test whether the two files have the same Ionde number, that is, whether the two files are hard links. (I will explain it later)
Add the logical operator number to the test statement:
1. Use the bash logical operator number
[Expression1] & [expression2] and
[Expression1] | [expression2] or
! [Expression1] non
2. Use the logical operator number of the conditional test command itself:
[Expression1-a expression2] and operations
[Expression1-o expression2] or operation
[! Expression1] Reverse
Command Execution result:
1. normal output result of the command;
2. Return Value of command execution status;
0-255
0: indicates that the command is successfully executed or the condition is true;
1-255: indicates that the command execution fails or the condition is false;
127,: reserved by default;
3-126,128-255: returned value of the custom Command Execution status;
Exit command:
Exit-exit the shell
Format:
Exit [n]
When you exit shell, you can use n as the return value. If n is not specified, the default return value is the return value of the last command;
Note: When the shell script runs, once the exit command is run, the shell process running the script will be ended immediately, at the same time, the execution of this script will also stop all subsequent commands and will not be interpreted and executed; therefore, the script containing the exit command should not be executed using the source command;
[Root @ localhost ~] # [-E/abcd]
[Root @ localhost ~] # Echo $?
1
[Root @ localhost ~] # [-E/abcd] | exit 5
[Root @ localhost ~] # Echo $?
5
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151486.htm