2020-08-25shell流程控制结构

2020-08-25  本文已影响0人  testerPM

条件表达式

概念:
在各种条件结构和流程控制结构进行的各种判断,然后根据不同条件执行不同的操作

[条件表达式]
注意: 1.括号内部注意两端要有空格 2. 操作符两端也要有空格
例子:
[ -f /tmp/hhy.txt ] 单个条件 ,判断是否存在
[ -f /tmp/hhy.txt ] && echo 1 || echo 0

注意: &&代表前面执行成功则执行后面的命令 ,|| 否则执行后面的

类型 关键字 含义解释
整数比较操作符 -eq、-gt、-lt、-ge、-le 等于/大于/小于/大于等于/小于等于
字符串测试操作符 ==、!=、-n、-z 等于/不等于/字符串长度不为0为真/字符串长度为0 为真
文件测试 -d、-f、-e 目录存在为真/文件存在为真/目录或者文件存在为真
逻辑操作符 !、-a、-o 非/与/或

整数比较操作符举例如下:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0

[  5   -eq  3 ] &&   echo  “true"  ||   echo ”false"


上面脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh
false

解释:

[  5   -eq  3 ] &&   echo  “true"  ||   echo ”false"

解释如下:
[  5   -eq  3 ] 判断5和3是否相等
 &&   echo  “true"   如果相等,就输出 true
||   echo ”false"  否则,输出false



字符串测试操作符举例如下:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0

[  "hello" ==  "world"   ] &&   echo  “true"  ||   echo ”false"
[   -n   "world"   ] &&   echo  “true"  ||   echo ”false"
[   -z   ""   ] &&   echo  “true"  ||   echo ”false"

上面脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh
false
true
true

解释如下:

[  "hello" ==  "world"   ] 字符串不相等,所以执行 echo ”false"
[   -n   "world"   ]   字符串长度不为 0 ,所以执行echo  “true" 
[   -z   ""   ] 字符串长度为0 ,所以执行echo  “true" 
 

文件测试脚本举例如下:


#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
[  -f  /home/yy.txt  ]    &&   echo  “true"  ||   echo ”false"
[  -d  /home/yy/  ]  &&   echo  “true"  ||   echo ”false"
[  -e  /home/yy.txt ]  &&   echo  “true"  ||   echo ”false"
[  -e /home/yy/  ]  &&   echo  “true"  ||   echo ”false"



上面脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh
true
true
true
true

解释:

[  -f  /home/yy.txt  ]    此目录下yy.txt文件存在  所以执行  echo  “true"
[  -d  /home/yy/  ]  此路径存在, 所以执行  echo  “true"
[  -e  /home/yy.txt ]   此目录下yy.txt文件存在  所以执行  echo  “true"
[  -e /home/yy/  ]    此路径存在, 所以执行  echo  “true"

备注: 1.-e 参数即可用户来判断路径也可以用来判断文件  2.路径必须是绝对路径
  

逻辑操作符脚本举例如下:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
[  !  -e /home/yy/  ]   &&   echo  “true"  ||   echo ”false"
[  -e  /home/yy.txt   -a  -z  "hello" ]     &&   echo  “true"  ||   echo ”false"
[  -e  /home/yy.txt   -o  -z  "hello" ]     &&   echo  “true"  ||   echo ”false"


上面脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh
false
false
true

解释:

[  !  -e /home/yy/  ]    -e /home/yy/ 判断目录存在,存在则为真,这里如果为真,取反就是假,表达式为假,所以执行  echo ”false"
[  -e  /home/yy.txt   -a  -z  "hello" ]    -e  /home/yy.txt 为真,   -z  "hello" 为假,用  -a 进行连接与计算-》结果为假,所以执行  echo ”false"
[  -e  /home/yy.txt   -o  -z  "hello" ]        -e  /home/yy.txt 为真,  -z  "hello" 为假,用 -o 进行连接或计算-》结果为真,所以执行  echo  “true"


流程控制结构

if条件语句

(1) 单分支结构:

         if   [  条件表达式  ]; then 
                            执行语句
         fi

备注:  
          1.  fi是结束符
         2.  分号和 ] 之间不用加空格
         3.   if 和  [  之间需要加空格 
          4.  执行语句需要tab缩进

举例说明:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
if  [ -e  /home/yy/ -o  -z "hello" ]: then
               echo "true"
fi 

上面脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh
true

(2)双分支结构

  if  [ 条件表达式 ]:  then
          执行语句1
else
           执行语句2
fi



举例说明:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
if  [ -e  /home/yy/ -a  -z "hello" ]: then
               echo "true"
else 
              echo "false"

上面脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh
false

(3)多分支结构

   if  [  条件表达式 ];then 
              执行语句
 elif [ 条件表达式 ]; then
             执行语句
...多个elif
else
               执行语句
fi

注意:  多分支elif的写法,每个elif都要带有then
             最后结尾的else 后面没有then

举例说明:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
if  [ "$1"  == "start" ];then
         echo "start  cmd"
elif [ "$1" == "stop" ]; then
       echo "stop cmd"
elif [ "$1" == "restart" ]; then
       echo " restart cmd"
else
       echo "other cmd"
fi

上面脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh  start  
start cmd


备注:
1.如果$1不加双引号,执行脚本不加参数会报错,因为,不加参数,$1获取到的结果是 null   (null对象不能和字符串比较)
 2.$1加上双引号,脚本执行不加参数(把null当成一个字符串),脚本会执行这条语句       echo "other cmd",脚本不会报错
 3. 进行字符串比较时,最好加上双引号
 

流程控制结构

(1) while循环语句:

重复执行一条语句或者一组语句,直到条件不再满足时停止
    while [ 条件表达式 ]
    do 
                     执行语句
    done

while 循环语句会对while后面的条件表达式进行判断,如果条件表达式成立,则执行do和done之间的指令
每一次执行到done时就会重新判断while条件表达式

脚本举例1如下:循环打印1-10

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
i=1
while [ $ i  -le  10 ]
do
        echo ${i}
          ((i++))

done
              

脚本执行:

[root@localhost sciptshell]#./firstShellScript.sh 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10



备注: ((i++)) 不能是 i++ 或者$(i++)  或者${1}++
这里使用((i++)),是因为((运算表达式))用户整数运算


脚本举例2如下:求和1+2+3.....+10

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
sum=0
i=1
while [ $ i  -le  10 ]
do
              sum=$((sum+i))
              ((i++))

done
              echo ${sum}

脚本执行结果:

[root@localhost sciptshell]#./firstShellScript.sh 
  55

备注:上面脚本    sum=$((sum+i)) 为什么不能是 
sum =${sum}+${i}

(2) for 循环语句

for 语句和while循环语句类似,for循环主要用于执行次数有限的循环,while循环一般用在守护进程以及无限循环

第一种

 for  变量名  in  变量取值列表
 do
                   指令
 done




变量名依次获取 in关键字后面的变量取值列表,每次取一个,然后进入循环执行指令,当取完变量列表中的最后一个值并进入循环执行到done为止

脚本举例说明如下:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
i=1
for i in 1 2 3 4 5 6 7 8 9 10
do
         echo ${i}
done



脚本执行:

[root@localhost sciptshell]#./firstShellScript.sh 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10


第二种

  for (( exp1;exp2;exp3))
  do
                   指令
  done


备注:
exp1: 变量初始化
exp2: 条件表达式
exp3: 变量更改条件表达式

脚本举例说明:

#!/bin/bash
#Date:      2017-01-01
#Author:   Created by hhy
#Email:  1425301899@qq.com
#Description:  The script function  is to.....
#Version:     1.0
for ((i=1; i<=10;i++))
do
            echo ${i}
done

#备注: i<=10为什么不是用 i -le 10

脚本执行:

[root@localhost sciptshell]#./firstShellScript.sh 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10


上一篇 下一篇

猜你喜欢

热点阅读