理论 : shell编程之条件语句————理论讲解

发布时间:2020-06-28 02:27:49 作者:wx5d8b05ec4cbc3
来源:网络 阅读:455

前言:
条件语句也是流程控制语句,日常生活逻辑

条件测试

if语句

一 :条件测试操作

1.1.1 test命令

'格式1:test 条件表达式
'格式2:[ 条件表达式 ]

在格式2中,前后至少有一个空格

1.1.2 文件测试 文件类型和权限测试

1.1.3 常用的测试操作符

[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 $? 查询上个步骤是否成立,成立则为0,不成立则为非0值

[gsy@localhost opt]$ [ -w /opt/abc ]&&echo "yes"

&& 是而且的含义 表示如果两边的条件都成立,才会正确执行;echo "yes"很明显是正确的,即若是[ -w /opt/abc ]成立,则会输出yes,若是不成立,则不会输出yes,这种操作可以变相地来验证操作是否成立

|| 或的含义,只要有一个成立,整体就算对;第一个对的话,就不会在去查看下面操作

[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 整数值比较

[ 整数1 操作符 整数2 ]

1.2.2 常用的测试操作符

[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

$()里面接命令 作用相当于··反撇符号

[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 字符串比较

1.3.2 常用的测试操作符

1.3.3 演示

[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

理论 : shell编程之条件语句————理论讲解

中括号内为测试语句,可以进行比较

是否创建/opt/share目录:(yes/no) 创建成功

已经存在

[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创建成功

一元运算符:

i=1;

i=i++ 等同于i=$i+1 ,代表先赋值,再加加,即并没有重新赋值

i=++i 先加再赋值 ,把得到的结果再去赋值,此时的加加就有了意义

[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]# 

二元运算符

a+b=c

三元运算符

条件&&结果1||结果2

条件成立执行结果1,不成立执行结果2

1.4.1 逻辑测试

格式1:

[ 表达式1 ] 操作符 [ 表达式2 ] ···

格式2:

命令1 操作符 命令2 ·······

1.4.2 常用的测试操作符

[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

二 : if语句的结构

2.1 单分支结构

理论 : shell编程之条件语句————理论讲解

fi结束判断,exit 0正常退出,exit 1异常退出

2.2 双分支结构

理论 : shell编程之条件语句————理论讲解

理论 : shell编程之条件语句————理论讲解

2.3 多分支结构

理论 : shell编程之条件语句————理论讲解

理论 : shell编程之条件语句————理论讲解

三 : if语句应用实例

3.1 单分支if语句


## 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]# 

3.3 多分支语句

理论 : shell编程之条件语句————理论讲解

elif 否则 如果

exit 1 异常退出

[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
不及格,还要好好努力啊少年

理论 : shell编程之条件语句————理论讲解

总结:

一个简易的计算器

#!/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

作业:田径赛决赛名单 淘汰名单 姓名 性别 成绩

[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

理论 : shell编程之条件语句————理论讲解

推荐阅读:
  1. 12c 禁用DBA权限,你怎么给业务用户授权
  2. Oracle 怎么创建一个性能最优,最安全的线上用户!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shell脚本进阶学习 条件语句 shell理论详解

上一篇:ARP地址解析协议<一>——地址解析

下一篇:用的较为舒服的时间插件jQuery DateTimePicker

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》