We have already learned to write a relatively simple shell script: each statement is executed in sequence, so as to realize the automatic process of "batch processing"
However, there are more "single" sequential structures written like this. Would you find it easier? In this way, the script is too mechanized, like a pipeline, not "smart" enough!
So, how can we calmly deal with complex system tasks?
Next, let’s learn how to perform conditional test operations, and through the correct use of if statements, so that the shell script has a certain "judgment" ability, so as to complete different management tasks according to different conditions
1. Condition test
1. Test command-test
To make the shell script program have a certain "smart", the first step is to distinguish between different situations to determine what operation to perform
The Shell environment judges whether the execution is successful according to the return status value ($?) after the command is executed . When it is 0, it is successful (indicating that the condition is established), otherwise (non-zero value) indicates failure or exception.
Example: Determine the size of the currently available free memory (free). If the current memory is less than 3333MB, it is recommended to clean it up in time (I have a higher configuration for the virtual machine here ==)
Determine the dependency between two or more conditions
When the system task depends on multiple different conditions, the judgment is based on whether these conditions are established at the same time or only one of them is established.
-a or &&: logical AND, meaning "and", both before and after conditions are met
-o or ||: logical OR, "or" meaning, one of the two is sufficient
! : Logically no, only when the specified condition is not established, the result is established
"&&", "||" operators can normally exist in the "[[]]" conditional judgment structure, but if they appear in the "[]" structure, an error will be reported
If the condition is true, execute the command after then, ignore the else, until the end of fi (here continue according to the previous template)
If the condition is not established, then ignore then, jump to the command sequence after else, until the end of fi
2.3 Multi-branch statement
##基本格式
if 条件测试操作1
then 命令序列1
elif 条件测试操作2
then 命令序列2
else
命令序列3
fi
[root@localhost ~]# vim ccc.sh
#!/bin/bash
read -p "请输入你的成绩(0~100):" cj
if [ $cj -ge 80 ] && [ $cj -le 100 ]
then
echo "优秀"
elif [ $cj -ge 60 ] && [ $cj -le 80 ]
then
echo "良好"
else
echo "不及格"
fi
Three, case branch statement
The case statement is mainly used in the following scenarios: when there are multiple values for each variable, a different sequence of commands needs to be executed for each value
The if statement is to judge multiple different conditions, and case judges the different values of a variable
1. Structure
##基本格式
case 变量值 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
....
*)
默认命令序列
esac
The end of the case line must be the word in, and each line must end with a bracket ")"
The double semicolon ";;" indicates the end of the command sequence
You can use square brackets "[]" to indicate a continuous range; you can also use "|" to indicate, such as "A|B"
The last ")*" means the default mode, if the above values are not the same, execute this line
2. Application examples
[root@localhost ~]# vim ddd.sh
#!/bin/bash
#不说了我要去罚抄了
read -p "请输入你的分数: " sub
case $sub in
100)
echo "恭喜你 逃过一劫"
;;
8[0-9]|9[0-9])
echo "每道错题抄10遍"
;;
6[0-9]|7[0-9])
echo "每道错题抄20遍"
;;
*)
echo "呵呵 全部30遍"
esac