shell 脚本之条件测试

Shell 编程之条件语句

一 ,条件测试



1.1 文件测试与整数测试

1.1.1 test 命令

测试表达式是否成立,若成立,则返回0,否则返回其他数值(返回值使用 echo $? 查看)

格式1: test 条件表达式

格式2: [ 条件表达式 ] *#条件表达式与中括号两边至少各有一个空格

image-20210709144843646

1.1.2 文件测试

[ 操作符 文件或目录 ]

注意: 中括号两边要有空格

文件运算符 释义 示例
-e filename 如果filename 存在,则为真 [ -e /etc/passwd]
-f filename 如果filename 存在,且为目录,则为真 [ -d /opt ]
-d filename 如果filename 存在 ,且为文件,则为真 [ -f /etc/passwd ]
-L filename 如果fiename 存在,且为软连接,则为真 [ -L /bin ]
-r filename 如果filename存在,且当前用户可读,则为真 [ -r /etc/passwd ]
-w filename 如果filename存在,且当前用户可写,则为真 [ -w /etc/passwd ]
-x filename 如果filename 存在,且当前用户可执行,则为真 [ -x /usr/bin/umake ]
-s filename 如果filename存在,且不为空文件,则为真 [ -s /etc/passwd ]
-S filename 如果filename 存在,且为空,则为真 [ -S /etc/passwd ]
filename1 -nt filename2 如果 filename 1 比 filename 2 新,则为真 [ /etc/passwd -nt /etc/group ]
filename1 -ot filename2 如果 filename1 比 filename2 旧, 则为真 [ /etc/passwd -ot /etc/group ]

image-20210709172900672



image-20210709162348330



1.1.3 整数值比较

[ 整数1 操作符 整数2 ]

测试符 释义
-eq 等于(Equal)
-ne 不等于(Not Equal)
-gt 大于(Greater Than)
-lt 小于(Lesser Than)
-le 小于或等于(Lesser or Equal)
-ge 大于或等于(Greater or Equal)

一般,当使用单中括号"[]"时,使用文字比较符号
使用双中括号"[[ ]]"时,使用字符比较符号 如">= ; > ; == ; <;<="
当在单中括号里使用字符比较符号时,需要在符号前面加上 转义符号 "\"

image-20210709172900672

image-20210709173019634



image


1.2 字符串测试与逻辑测试



1.2.1 字符串比较

格式1:
[ 字符串1 = 字符串2 ] #字符串一样为真

​ [ 字符串1 != 字符串2 ] #字符串不一样为真

格式2:
[ -z 字符串 ] #字符串为空,则为真

​ [ -n 字符串 ] #字符串不为空,则为真

操作符 释义
= 测试字符串是否一样,一样为真
!= 测试字符串是否不一样,不一样为真
-z 测试字符串是否为空,为空则为真
-n 测试字符串是否不为空,不为空则为真
[root@host103 test]# a=abc
[root@host103 test]# b=def
[root@host103 test]# c=""
[root@host103 test]# [ $a = $b ]
[root@host103 test]# echo $?
1
[root@host103 test]# [ $a != $b ]
[root@host103 test]# echo $?
0
[root@host103 test]# [ -z $a ]
[root@host103 test]# echo $?
1
[root@host103 test]# [ -n $a ]
[root@host103 test]# echo $?
0
[root@host103 test]# 

image-20210709163805160



1.2.2 逻辑测试

格式:
[ 表达式1 ] 操作符 [ 表达式2 ] ....

格式2:
命令1 操作符 命令2

操作符 释义
-a 或者 && 逻辑与,“而且”的意思
-o 或者 || 逻辑或,“或者” 的意思
逻辑否(逻辑非)

image-20210709171544605

&& ,逻辑与,前面的条件都执行成功,才只执行后面的



image-20210709172115423

|| ,逻辑或,多个条件任意一个成功即可执行后面的



单中括号"[ ]"内,多个逻辑判断,使用 文字操作符 "-a" "-o"
两个中括号之间 "[ ] [ ]" 逻辑测试,使用字符操作符 " &&" "||"
双中括号之间"[[ ]]" 逻辑测试,使用字操作符 "&&""||"




image


二,if语句



2.1 if 单分支语句

格式:

if  条件测试
  then   命令序列
fi

image-20210711172655370

[root@host103 ~]# vim abc.sh
#!/bin/bash
read -p "input hi: " args
if [ $args == hi ];then      #如果输入的字符为“hi ”
    echo "hello"             #则打印“hello”
fi
:wq

image-20210711173425673



2.2 if 多分支语句

格式:

if  条件判断
    then   命令序列1
    else   命令序列2
fi

image-20210711173732134

[root@host103 ~]# vim abc.sh
#!/bin/bash
read -p "input hi: " args
if [ $args == hi ];then
    echo "hello"
else
    echo "please input hi"
fi
:wq   

image-20210711174255588



2.3 if 多分支语句

格式:

if 条件测试
   then 命令序列1
elif  条件测试2
   then  命令序列2
elif  条件测试3
    then 命令序列3
  ......
else
   命令序列4
fi

image-20210711175829068

[root@host103 ~]# vim abc.sh
#!/bin/bash
read -p "input your score :" num
if [ $num -ge 90 ];then
    echo "great"
elif [ $num -ge 80 ] ;then
    echo "good"
elif [ $num -ge 70 ] ;then
    echo "not bad"
elif [ $num -ge 60 ] ;then
    echo "pass"
else
    echo "fail"
fi
:wq

image-20210711192007152



三,case语句

case 可以用来判断一个变量的不同取值

格式:

case  $变量  in
值1)
	命令序列1 ;;
值2)
  命令序列2 ;;
*)
  命令序列3 ;;
esac
  

image-20210711194645390

[root@host103 ~]# vim test.sh
#!/bin/bash
read -p "Do you want to know the meaning of life: " args
case $args in
yes)
    echo "you choise yes" ;;
no)
    echo "you choise no" ;;
*)                                         #case 值没有匹配,则执行此项
    echo "please input /yes/no";;
esac

image-20210711195141771

posted @ 2021-07-11 19:58  知己一语  阅读(170)  评论(0编辑  收藏  举报