Conditional statements
Arithmetic comparison
Conditions are usually placed within enclosing square brackets. Be sure to note that there is a space between [ or ] and the operand . If you forget this space, the script will report an error. For example:
[ $var-eq 0 ] When var is equal to 0, return true; [ $var-ne 0 ] When var is non-zero, return true
Important operators:
grammatical role-gt | more than the. |
-lt | less than. |
-give | greater than or equal to. |
-the | less than or equal to |
-eq | equal |
-born | not equal to |
Tests can be performed in combination with multiple conditions as follows:
- [ $var1 -ne 0 -a $var2 -gt 2 ] # Use logical AND -a
- [ $var -ne 0 -o $var2 -gt 2 ] # logical or -o
string comparison
When using string comparisons, it's best to use double square brackets , because sometimes a single square bracket can produce errors, so it's best to avoid them.
Two strings can be checked to see if they are the same.
[[ $str1 = $str2 ]] | Returns true when $str1 is equal to $str2. That is, $str1 and $str2 contain exactly the same text. |
[[ $str1 == $str2 ]] | This is another way to check if strings are equal. It is also possible to check if two strings are different. |
[[ $str1 != $str2 ]] | Returns true if $str1 and $str2 are not the same. We can also check the alphabetical order of the string as shown below. |
[[ $str1 > $str2 ]] | Returns true if $str1 is alphabetically greater than $str2. |
[[ $str1 < $str2 ]] | Returns true if $str1 is less alphabetically than $str2. |
[[-z $ str1]] | Returns true if $str1 contains the empty string. |
[[ -n $str1 ]] | Returns true if $str1 contains a non-empty string. |
Multiple conditions can easily be combined using the logical operators && and || ; if [[ -n $str1 ]] && [[ -z $str2 ]], the semantics of not if [[ ! -n $str1 ] ] ; pay attention to spaces
File system
grammatical role[ -f $file_var ] | Returns true if the given variable contains a normal file path or file name . |
[ -d $var ] | Returns true if the given variable contains a directory. |
[ -e $var ] | Returns true if the file contained in the given variable exists. |
[ -w $var ] | Returns true if the file contained in the given variable is writable. |
[ -r $var ] | Returns true if the file contained in the given variable is readable. |
[ -c $var ] | Returns true if the given variable contains the path to a character device file. |
[ -b $var ] | Returns true if the given variable contains the path to a block device file. |
[-x $ var] | Returns true if the file contained in the given variable is executable. |
[ -L $var ] | if the given variable contains a symbolic link |
control statement
if statement
We can perform tests with if, if else, and logical operators, and compare data items with some comparison operators.
if [ condition] then commands else commands fi |
It is worth noting that if then and if judgments are on the same line, you need to add ";"
for loop
Form 1
Values are separated by spaces
for variable in XXX do program done # example 1 for item in 1 2 3 do echo $item done # example 2 for item in $(ls ~) do echo $item done # example 3 for variable in `seq 1 100` do program done |
Form 2
with seq
for ((initial value; control condition; variable change)) do program done |
while loop
while [conditional judgment] do program done |
with special symbols
symbolic role‘’ | apostrophe. All special symbols such as "$" and "`" (backticks) in single quotes have no special meaning. |
“” | Double quotes. Special symbols in double quotes have no special meaning, but "$", "`" and "\" are exceptions, which have special meanings for "calling the value of a variable", "quoting a command" and "escape character". |
`` | backticks. The content enclosed in backticks is a system command, which is executed first in Bash. Same as $(), but $() is recommended because backticks are easy to read wrong. |
$() | Like backticks, it is used to quote system commands. |
# | In shell scripts, lines starting with # represent comments. |
$ | It is used to call the value of the variable. If you need to call the value of the variable name, you need to use $name to get the value of the variable. |
\ | 转义符,跟在\之后的特殊符号将失去特殊含义,变为普通字符。如\$将输出“$”符号,而不当做是变量引用。 |