Preface:
conditional statements also flow control statements, the logic of everyday life
Test conditions
- File test
- Integer test
- Logic test string
The if statement
- if a single branch statement
- if two-branch statement
- if multi-branch statement
A: which test
1.1.1 test command
- Test specific expression is established when the condition is met, the test statement returns 0, otherwise other values
'格式1:test 条件表达式
'格式2:[ 条件表达式 ]
In Format 2, before and after at least one space
1.1.2 file permissions and file type test test
-
[ 操作符 文件或目录 ]
1.1.3 Common test operators
- -e: test directory or file exists (Exist)
- -d: test whether a directory (Directory)
- -f: test whether the file (File)
- -r: to test whether the current user has permission to read (Read)
- -w: Testing whether the current user has permission to write (Write)
- -x: Testing whether the current user has permission to perform (eXcute)
[root@localhost opt]# touch test.txt
[root@localhost opt]# mkdir abc
[root@localhost opt]# ls
abc rh test.txt wwwroot
[root@localhost opt]# test -d /opt/abc/
[root@localhost opt]# echo $?
0
[root@localhost opt]# [ -d /opt/abc ]
[root@localhost opt]# echo $?
0
[root@localhost opt]# test -f /opt/abc
[root@localhost opt]# echo $?
1
[root@localhost opt]# test -f /opt/test.txt
[root@localhost opt]# echo $?
0
[root@localhost opt]# test -f /opt/te.txt
[root@localhost opt]# echo $?
1
[root@localhost opt]# [ -x /opt/abc]
bash: [: 缺少 `]'
[root@localhost opt]# [ -x /opt/abc ]
[root@localhost opt]# ls -al
总用量 16
drwxr-xr-x. 5 root root 156 11月 26 13:51 .
dr-xr-xr-x. 17 root root 224 10月 23 13:41 ..
'drwxr-xr-x. 2 root root 6 11月 26 13:51 abc
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
-rw-r--r--. 1 root root 0 11月 26 13:51 test.txt
[gsy@localhost opt]$ [ -w /opt/abc ]
[gsy@localhost opt]$ echo $?
1
[gsy@localhost opt]$ [ -w /opt/abc ]&&echo "yes"
[gsy@localhost opt]$ echo $?
1
[gsy@localhost opt]$ [ -r /opt/abc ]&&echo "yes"
yes
[gsy@localhost opt]$ echo $?
0
[gsy@localhost opt]$
echo $? query on whether the step has founded or 0, non-zero value was not established
[gsy@localhost opt]$ [ -w /opt/abc ]&&echo "yes"
&& is the meaning and said that if both sides are true, will be performed correctly; echo "yes" is clearly correct, that is, if [-w / opt / abc] set up, it will output yes, if not established, it is not outputs yes, this operation can be disguised to verify whether the operation was established
|| or meaning, as long as there is a set up, even for overall; first on, then it will not go see below operating
[root@localhost opt]# [ -d /opt/abc ]|| echo "year"
[root@localhost opt]# echo $?
0
[root@localhost opt]# [ -d /opt/ab ]|| echo "year"
year
[root@localhost opt]# echo $?
0
[root@localhost opt]# [ -d /opt/abc ]|| echo "year"
[root@localhost opt]# echo $?
0
1.2.1 Comparison integer
[ 整数1 操作符 整数2 ]
1.2.2 Common test operators
- -eq: equal (Equal)
- -ne: not equal (Not Equal)
- -gt: greater than (Greater Than)
- -lt: less than (Lesser Than)
- -le: less than or equal to (Lesser or Equal)
- -ge: greater than or equal to (Greater or Equal)
[root@localhost opt]# [ 5 -gt 3 ]&& echo "yes"
yes
[root@localhost opt]# [ 5 > 3 ]&& echo "yes"
yes
[root@localhost opt]# [ 5 < 3 ]&& echo "yes"
yes
[root@localhost opt]# [ 5 \< 3 ]&& echo "yes"
[root@localhost opt]# echo $?
1
[root@localhost opt]# [ 5 \= 3 ]&& echo "yes"
[root@localhost opt]# [ 5 \> 3 ]&& echo "yes"
yes
[root@localhost opt]# [ 3 \= 3 ]&& echo "yes"
yes
[root@localhost opt]# [ 3 == 3 ]&& echo "yes"
yes
[root@localhost opt]# [ 3 != 3 ]&& echo "yes"
[root@localhost opt]# echo $?
1
[root@localhost opt]# [ 3 != 4 ]&& echo "yes"
yes
[root@localhost opt]# [ 3 >= 3 ]&& echo "yes"
bash: [: 3: 期待一元表达式
[root@localhost opt]# [ 3 => 3 ]&& echo "yes"
bash: [: 3: 期待一元表达式
[root@localhost opt]# [ 3 \>= 3 ]&& echo "yes"
bash: [: >=: 期待二元表达式
[root@localhost opt]#
[root@localhost opt]# who
root :0 2019-11-26 08:16 (:0)
root pts/0 2019-11-26 08:16 (:0)
[root@localhost opt]#
[root@localhost opt]# who |wc -l
2
[root@localhost opt]# [ $(who |wc -l) -lt 5 ]&& echo "too less"
too less
[root@localhost opt]# [ $(who |wc -l) -ge 2 ]&& echo ">=2"
>=2
$ () Command which then acts as the prime symbol · trans
[root@localhost opt]# free -m
total used free shared buff/cache available
Mem: 1984 686 81 9 1216 1060
Swap: 2047 0 2047
[root@localhost opt]# free -m | grep Mem
Mem: 1984 686 81 9 1216 1060
[root@localhost opt]# free -m | grep Mem | awk '{print $1,$3,$4}'
Mem: 686 81
[root@localhost opt]# free -m | grep Mem | awk '{print $4}'
79
[root@localhost opt]# abc=$(free -m | grep Mem |awk '{ print $4 }')
[root@localhost opt]# echo abc
abc
[root@localhost opt]# echo $abc
77
[root@localhost opt]# [ $abc -gt 1024 ]&& echo "yes"
[root@localhost opt]# [ $abc -gt 50 ]&& echo "yes"
yes
1.3.1 string comparison
-
Format 1:
[ 字符串1 = 字符串2 ] [ 字符串1 != 字符串2 ]
-
Format 2:
[ -z 字符串 ]
1.3.2 Common test operators
- =: The same string content
- ! =: Different string content! He represents the opposite meaning
- -z: the string is empty
- String marked out with double quotes
1.3.3 demo
[root@localhost opt]# echo $LANG
zh_CN.UTF-8
[root@localhost opt]# [ $LANG = "zh_CN.UTF-8" ]&& echo "yes"
yes
[root@localhost opt]# [ $LANG != "zh_CN.UTF-8" ]&& echo "yes"
[root@localhost opt]# echo $?
1
[root@localhost opt]# [ "男" != "男" ]&& echo "yes"
[root@localhost opt]# [ "男" = "男" ]&& echo "yes"
yes
[root@localhost opt]# [ "男" = "女" ]&& echo "yes"
[root@localhost opt]# [ "男" != "女" ]&& echo "yes"
yes
To test the statements within the brackets can be compared
Whether to create / opt / share directory: (yes / no) successfully created
already exists
[root@localhost opt]# ls -al
总用量 16
drwxr-xr-x. 5 root root 156 11月 26 13:51 .
dr-xr-xr-x. 17 root root 224 10月 23 13:41 ..
'drwxr-xr-x. 2 root root 6 11月 26 13:51 abc
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
-rw-r--r--. 1 root root 0 11月 26 13:51 test.txt
[root@localhost opt]# mv test.txt test.sh
[root@localhost opt]# vim test.sh
#!/bin/bash
read -p "是否创建/opt/share目录:(yes/no)" ack
[ $ack = "yes" ]&& mkdir /opt/share
echo "创建成功"
[root@localhost opt]# sh test.sh
是否创建/opt/share目录:(yes/no)yes
创建成功
[root@localhost opt]# ls
abc share test.sh
rh
[root@localhost opt]# vim test.sh
#!/bin/bash
read -p "是否创建/opt/demo目录:(yes/no)" ack
[ -d /opt/demo ]&&echo "/opt/demo已经存在" || mkdir /opt/demo && echo "/opt/demo创建成功"
[root@localhost opt]# sh test.sh
是否创建/opt/demo目录:(yes/no)yes
/opt/demo创建成功
[root@localhost opt]# sh test.sh
是否创建/opt/demo目录:(yes/no)yes
/opt/demo已经存在
/opt/demo创建成功
Unary operators:
i=1;
i = i ++ is equivalent to i = $ i + 1, representative of first assignment, then jerk, i.e., no re-assignment
i = ++ i before adding another assignment, the result of the assignment again, this time there will be a sense of Gaga
[root@localhost opt]# i=1
[root@localhost opt]# echo $i
1
[root@localhost opt]# i++
bash: i++: 未找到命令...
[root@localhost opt]# i++;
bash: i++: 未找到命令...
[root@localhost opt]# expr i++
i++
[root@localhost opt]# let i++
[root@localhost opt]# echo $i
2
[root@localhost opt]# let i=i++
[root@localhost opt]# echo $i
2
[root@localhost opt]# let i++
[root@localhost opt]# echo $i
3
[root@localhost opt]# let ++i
[root@localhost opt]# echo $i
4
[root@localhost opt]# let i=++i
[root@localhost opt]# echo $i
5
[root@localhost opt]# let i=i++
[root@localhost opt]# echo $i
5
[root@localhost opt]# let i=++i
[root@localhost opt]# echo $i
6
[root@localhost opt]# let i=i+
bash: let: i=i+: 语法错误: 期待操作数 (错误符号是 "+")
[root@localhost opt]# let i=i++
[root@localhost opt]# echo $i
6
[root@localhost opt]# let i+=2
[root@localhost opt]# echo $i
8
[root@localhost opt]#
[root@localhost opt]# i\*=2
bash: i*=2: 未找到命令...
[root@localhost opt]# let i\*=2
[root@localhost opt]# echo $i
16
[root@localhost opt]# let i/=2
[root@localhost opt]# echo $i
8
[root@localhost opt]# let i%=2
[root@localhost opt]# echo $i
0
[root@localhost opt]# echo $i
0
[root@localhost opt]#
Binary operators
a+b=c
Ternary operator
Results Results && condition. 1 2 ||
Results 1 execution condition is satisfied, does not hold the execution result 2
1.4.1 logic test
Format 1:
[ 表达式1 ] 操作符 [ 表达式2 ] ···
Format 2:
命令1 操作符 命令2 ·······
1.4.2 Common test operators
- or -a &&: logical AND, "and" means
- or -o ||: logical OR, "or" means
- ! : No logic, on behalf of inverted meaning
[root@localhost opt]# [ -d /etc ]&& [ -r /etc ]&&echo "you can open /etc this diretory"
you can open /etc this diretory
[root@localhost opt]# [ -d /etc ]|| [ -d /home ]&& echo "ok"
ok
[root@localhost opt]# [ -r /etc ]|| [ -d /home ]&& echo "ok"
ok
[root@localhost opt]# [ -r /etc ]|| [ -r /home ]&& echo "ok"
ok
[root@localhost opt]# [ -f /etc ]|| [ -f /home ]&& echo "ok"
[root@localhost opt]# echo $?
1
[root@localhost opt]# [ ! -f /etc ]|| [ -f /home ]&& echo "ok"
ok
II: Structure of the if statement
2.1 single-branch structure
End Analyzing fi, exit 0 normal exit, exit 1 abnormal exit
2.2 pairs of branch structure
More than 2.3 branch structure
Three: if statement application examples
3.1 single-branch if statement
- Judgment mount point directory, if there is automatically created
[root@localhost opt]# vim test02.sh #!/bin/bash dir="/opt/demo02" if [ ! -d $dir ] then mkdir -p $dir echo "$dir创建成功" fi [root@localhost opt]# sh test02.sh /opt/demo02创建成功 [root@localhost opt]# ls [root@localhost opt]# ls abc share test.sh rh demo02 [root@localhost opt]# vim test02.sh #!/bin/bash dir="/opt/demo02" if [ ! -d $dir ] then mkdir -p $dir echo "$dir创建成功" else echo "$dir已存在" fi [root@localhost opt]# sh test02.sh /opt/demo02已存在
## 3.2 双分支if语句
- 判断目标主机是否存活,显示检测结果
- ping -c 发送包个数 -i 间隔时间,单位s,-W 等待3s
$1 位置变量ip地址,把结果混合输出到null中
上一条结果若是等于0,成立,则输出up,else就会down
![](https://s1.51cto.com/images/blog/201911/26/a02e04631aec4534762c49c2f54bbe34.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
请输入IP地址:
```shell
[root@localhost opt]# vim test02.sh
#!/bin/bash
read -p "请输入IP地址:" addr
ping -c 3 -i 0.2 -W 3 $addr &> /dev/null
if [ $? -eq 0 ]
then
echo "$addr is up"
else
echo "$addr is down"
fi
[root@localhost opt]# sh test02.sh
请输入IP地址:192.168.139.132
192.168.139.132 is up
[root@localhost opt]# sh test02.sh
请输入IP地址:139.168.139.133
139.168.139.133 is down
[root@localhost opt]#
More than 3.3 branch statement
Otherwise, if elif
exit 1 abnormal exit
[root@localhost opt]# vim fenshu.sh
#!/bin/bash
read -p "请输入您的分数" score
if [ $score -lt 0 ]
then
echo "你已经没救了"
elif [ $score -gt 100 ]
then
echo "别做梦"
elif [ $score -ge 85 ]
then
echo "成绩优秀"
elif [ $score -lt 70 ]
then
echo "不及格,还要好好努力啊少年"
else
echo "成绩合格,不要就此止步,继续努力!"
fi
~
[root@localhost opt]# sh fenshu.sh
请输入您的分数-9
你已经没救了
[root@localhost opt]# sh fenshu.sh
请输入您的分数101
别做梦
[root@localhost opt]# sh fenshu.sh
请输入您的分数90
成绩优秀
[root@localhost opt]# sh fenshu.sh
请输入您的分数60
不及格,还要好好努力啊少年
to sum up:
- Syntax condition test operation
- Test file, compare an integer value, a string comparison logic test
- The syntax diagram if conditional statement
- Single branch, double-branch, multi-branch
A simple calculator
#!/bin/bash
read -p "请输入一个整数:" numb1
read -p "请选择你需要的运算;选项:加(+)减(-)乘(x)除(/)取余 (%)" yunsuan
read -p "请输入第二个整数:" numb2
if [ "$yunsuan" = "+" ]
then
expr=`expr $numb1 + $numb2`
echo "$numb1 + $numb2 = $expr"
elif [ "$yunsuan" = "-" ]
then
expr=`expr $numb1 - $numb2`
echo "$numb1 - $numb2 = $expr"
elif [ "$yunsuan" = "x" ]
then
expr=`expr $numb1 \* $numb2`
echo "$numb1 x $numb2 = $expr"
elif [ "$yunsuan" = "/" ]
then
expr=`expr $numb1 / $numb2`
echo "$numb1 / $numb2 = $expr"
else
expr=`expr $numb1 % $numb2`
echo "$numb1 % $numb2 = $expr"
fi
~
~
[root@localhost opt]# sh jisuanqi.sh
请输入一个整数:10
请选择你需要的运算;选项:加(+)减(-)乘(x)除(/)取余 (%)+
请输入第二个整数:8
10 + 8 = 18
[root@localhost opt]# vim jisuanqi.sh
[root@localhost opt]# sh jisuanqi.sh
请输入一个整数:10
请选择你需要的运算;选项:加(+)减(-)乘(x)除(/)取余 (%)-
请输入第二个整数:5
10 - 5 = 5
[root@localhost opt]# sh jisuanqi.sh
请输入一个整数:10
请选择你需要的运算;选项:加(+)减(-)乘(x)除(/)取余 (%)x
请输入第二个整数:2
[root@localhost opt]# sh jisuanqi.sh
请输入一个整数:17
请选择你需要的运算;选项:加(+)减(-)乘(x)除(/)取余 (%)%
请输入第二个整数:4
17 % 4 = 1
[root@localhost opt]# sh jisuanqi.sh
请输入一个整数:10
请选择你需要的运算;选项:加(+)减(-)乘(x)除(/)取余 (%)/
请输入第二个整数:5
10 / 5 = 2
Job: track and field finalist out of the list Name Sex scores
[root@localhost opt]# vim tianjingsai.sh
1 #!/bin/bash
2 fnan=/opt/nanzizujuesaimingdan
3 fnv=/opt/nvzizujuesaimingdan
4 ftao=/opt/taotaimingdan
5 if [ ! -f $fnan ]&& [ ! -f $fnv ]&& [ ! -f $ftao ]
6 then
7 touch $fnan $fnv $tao
8 fi
9 read -p "请输入(格式:姓名 性别 成绩):" xingming xingbie chengji
10 if [ $chengji -lt 0 ]
11 then
12 echo "???你在输什么??"
13 exit 1
14 elif [ $chengji -gt 0 ] && [ $chengji -lt 10 ]
15 then
16 echo "你的成绩优秀,可以进入10秒内决赛"
17 if [ $xingbie = "nan" ]
18 then
19 echo "$xingming $xingbie $chengji" >> /opt/nanzizujuesaimingdan
20 else
21 echo "$xingming $xingbie $chengji" >> /opt/nvzizujuesaimingdan
22 fi
23 else
24 echo "$xingming $xingbie $chengji" >> /opt/taotaimingdan
25 echo "再加把劲,下次就是你了"
26
27 fi
~
[root@localhost opt]# sh tianjingsai.sh
请输入(格式:姓名 性别 成绩):gsy nan 1
你的成绩优秀,可以进入10秒内决赛
[root@localhost opt]# sh tianjingsai.sh
请输入(格式:姓名 性别 成绩):zzz nv 6
你的成绩优秀,可以进入10秒内决赛
[root@localhost opt]# sh tianjingsai.sh
请输入(格式:姓名 性别 成绩):aaa nan -6
???你在输什么??
[root@localhost opt]# sh tianjingsai.sh
请输入(格式:姓名 性别 成绩):aaa nan 14
再加把劲,下次就是你了
[root@localhost opt]# ls
fenshu.sh nvzizujuesaimingdan taotaimingdan test.sh wwwroot
nanzizujuesaimingdan rh test02.sh tianjingsai.sh
[root@localhost opt]# cat nvzizujuesaimingdan
zzz nv 6
[root@localhost opt]# cat nanzizujuesaimingdan
gsy nan 1
[root@localhost opt]# cat taotaimingdan
gsy nan 0.5
aaa nan 14