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

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

  • 文件测试
  • 整数测试
  • 字符串与逻辑测试

if语句

  • if单分支语句
  • if双分支语句
  • if多分支语句

一 :条件测试操作

1.1.1 test命令

  • 测试特定的表达式是否成立,当条件成立时,测试语句的返回值为0,否则为其他数值
'格式1:test 条件表达式
'格式2:[ 条件表达式 ]

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

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

  • [ 操作符 文件或目录 ]
    

1.1.3 常用的测试操作符

  • -e :测试目录或文件是否存在(Exist)
  • -d :测试是否为目录(Directory)
  • -f :测试是否为文件(File)
  • -r :测试当前用户是否有权限读取(Read)
  • -w :测试当前用户是否有权限写入(Write)
  • -x :测试当前用户是否有权限执行(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 $? 查询上个步骤是否成立,成立则为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 常用的测试操作符

  • -eq :等于(Equal)
  • -ne :不等于(Not Equal)
  • -gt :大于(Greater Than)
  • -lt :小于(Lesser Than)
  • -le :小于或等于(Lesser or Equal)
  • -ge :大于或等于(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

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

[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:

    [ 字符串1 = 字符串2 ]
    [ 字符串1 != 字符串2 ]
    
  • 格式2:

    [ -z 字符串 ]
    

1.3.2 常用的测试操作符

  • = :字符串内容相同
  • != :字符串内容不同,!表示相反的意思
  • -z : 字符串内容为空
  • 字符串需要用双引号标出来

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

在这里插入图片描述
中括号内为测试语句,可以进行比较

是否创建/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 常用的测试操作符

  • -a或&& :逻辑与,“而且”的意思
  • -o或|| :逻辑或,“或者”的意思
  • ! :逻辑否,代表取反含义
[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 单分支结构

在这里插入图片描述

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

2.2 双分支结构

在这里插入图片描述

在这里插入图片描述

2.3 多分支结构

在这里插入图片描述
在这里插入图片描述

三 : if语句应用实例

3.1 单分支if语句

  • 判断挂载点目录,若不存在则自动创建
    在这里插入图片描述
[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

在这里插入图片描述

请输入IP地址:

[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 多分支语句

在这里插入图片描述
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
不及格,还要好好努力啊少年

在这里插入图片描述

总结:

  • 条件测试操作的语法
    • 文件测试、整数值比较、字符串比较、逻辑测试
  • 图示if条件语句的语法
    • 单分支、双分支、多分支

一个简易的计算器

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

在这里插入图片描述

发布了87 篇原创文章 · 获赞 26 · 访问量 4558

猜你喜欢

转载自blog.csdn.net/Lfwthotpt/article/details/103263030