grammar
1. Basic grammar:
if [ command ]; then
# 符合该条件执行的语句
fi
1.1 Use else:
if [ command ]; then
# 符合该条件执行的语句1
elif [ command ]; then
# 符合该条件执行的语句2
else
# 符合该条件执行的语句3
fi
Syntax description:
bash shell
Theif
statements will be executed in order . If the command is executed and its return status is 0, the statement that meets the conditions will be executed, otherwise the subsequent commands will not be executed and skip to the next command.- When there are multiple nests, only the first command that returns an exit status of 0 will cause the part of the statement that meets the condition to be executed. If the execution status of all statements is not 0, the
else
statement is being executed .- Return status : The exit status of the last command, or 0 when none of the conditions are true.
note:
1. It
[]
means conditional test. There must be spaces in the[
back and in]
front.
2. Inshell
,then
andfi
are separate sentences. If you want to enter in the same line, you need to separate them with a semicolon.
3. Pay attention to the processing of variables in theif
judgment . You need to add quotation marks.
4. The judgment does not support floating point values .
5. If you only use the>
or<
number alone , the system It will be considered to be output or input redirection. Although the result is displayed correctly, it is actually wrong. Therefore, these symbols must be converted.
6.if
The error message generated by the command in the running statement will be displayed in the output result of the script by default.
7. When using-z
or-n
checking the length, undefined variables are also 0.
8. Empty variables and uninitialized variables may have a catastrophic impact on shell script testing. Therefore, if you are not sure about the content of the variable, you should Use-n
or-z
test before using the script
9. The?
variable contains the exit status of the previously executed command (recently completed foreground process), which can be used to detect the exit status
Common parameters:
- File/Directory Judgment
1.1 Commonly used
1.2 Uncommonly used- String judgment
- Numerical judgment
- Logical judgment
- Other judgments
if
Advanced features
1. File/directory judgment:
1.1 Commonly used:
[ -a FILE ]
True if FILE exists[ -d FILE ]
Return true if FILE exists and is a directory[ -e FILE ]
Returns true if the specified file or directory exists[ -f FILE ]
If FILE exists and is a normal file , return true[ -r FILE ]
Return true if FILE exists and is readable[ -w FILE ]
Return true if FILE exists and is writable[ -x FILE ]
Returns true if FILE exists and is executable
1.2 Not commonly used:
[ -b FILE ]
Return true if FILE exists and is a block file[ -c FILE ]
If FILE exists and is a character file , return true[ -g FILE ]
If FILE exists and SGID is set , return true[ -h FILE ]
If FILE exists and is a soft link file, it returns true[ -k FILE ]
If FILE exists and the risk bit has been set , return true[ -p FILE ]
Return true if FILE exists and is a command pipeline[ -s FILE ]
If the FILE exists and the size is not 0 , it is true and the return value is true[ -u FILE ]
If FILE exists and the SUID bit is set , return true[ -O FILE ]
If FILE exists and is a valid user ID , return true[ -G FILE ]
Returns true if FILE exists and the default group is the current group[ -L FILE ]
Returns true if FILE exists and is a symbolic link[ -S FILE ]
If FILE exists and is a socket , return true[ FILE1 -nt FILE2 ]
If FILE1 is newer than FILE2 , or FILE1 exists but FILE2 does not exist , return true- |
[ FILE1 -ot FILE2 ]
If FILE1 is older than FILE2 , or FILE2 exists but FILE1 does not exist , return true [ FILE1 -ef FILE2 ]
If FILE1 and FILE2 point to the same device and node number , return true
2. String judgment
[ -z STRING ]
If the length of STRING is zero, it returns true, that is, empty is true[ -n STRING ]
If the length of STRING is non-zero, it returns true, that is, non-empty is true[ STRING1 ]
If the string is not empty, it returns true,-n
similar to[ STRING1 == STRING2 ]
If the two strings are the same , return true[ STRING1 =~ pattern ]
Regular string matching[ STRING1 != STRING2 ]
If the strings are not the same , return true[ STRING1 < STRING2 ]
If "STRING1" is dictionary sorted before "STRING2" , it returns true.[ STRING1 > STRING2 ]
If the dictionary sort "STRING1" is after "STRING2" , it will return true.
3. Numerical judgment
[ INT1 -eq INT2 ]
INT1
AndINT2
two numbers are equal to return true,=
[ INT1 -ne INT2 ]
INT1
AndINT2
two numbers are not equal to return true,<>
[ INT1 -gt INT2 ]
INT1
Greater thanINT2
return true,>
[ INT1 -ge INT2 ]
INT1
Greater than or equal toINT2
return true,>=
[ INT1 -lt INT2 ]
INT1
Less thanINT2
returns true,<
[ INT1 -le INT2 ]
INT1
Less than or equal toINT2
return true,<=
4. Logical judgment
[ ! EXPR ]
Non-logic , ifEXPR
afalse
return to true[ EXPR1 -a EXPR2 ]
Logic and , ifEXPR1
theEXPR2
return is completely true is true[ EXPR1 -o EXPR2 ]
Or logic , ifEXPR1
orEXPR2
returns true true[ ] || [ ]
Use OR to combine two conditions[ ] && [ ]
Use AND to combine two conditions
5. Other judgments
[ -t FD ]
If the file descriptorFD
(default value is 1) is open and points to a terminal, it returns true[ -o optionname ]
If the shell option optionname is turned on, it returns true
6. if
Advanced features:
-
Double parentheses
(( ))
: that mathematical expression
double parentheses provide more mathematical symbols, and in double parenthesis>
with<
numbers not need to escape. -
Double brackets
[[ ]]
: indicate advanced string processing functions
. The judgment commands in the double brackets use standard string comparisons, and you can also use matching modes to define regular expressions that match the strings.
The role of double brackets :
in the shell, you can't write[ $a != 1 || $b = 2 ]
, you must write[ $a != 1 ] || [ $b = 2 ]
, or write[[ $a != 1 || $b = 2 ]]
. For example, this[ "$a" -lt "$b" ]
can also be changed to double brackets(("$a" < "$b"))