shell 判断与控制(二)

目录

3-1 使用if-then语句

3-2 使用if-then-else语句

3-3 嵌套if

3-4 条件测试-数值比较

3-5 条件测试-字符串比较

3-6 条件测试-文件比较

3-7 复合条件测试

3-8 if-then中使用双括号

3-8 if-then中使用双方括号

3-10 case命令


小结:单方括号中的条件只能写一个,有多个逻辑需要用多个单方括号,单方括号之间用逻辑运算符。单方括号的空格问题。

双括号,类C语言的表达式。双方括号,类C语言表达式,可写多个逻辑,但是仍要注意空格,引用。

3-1 使用if-then语句

if command | condition

then

     commands 

fi

if后面跟command的情况,只有当命令执行成功了,即退出状态码为0,才会执行then中的语句。

#!bin/bash
  
if pwd
then
    echo "It works"
fi

输出

/Users/qq_xs/Project/shell/ch3
It works

3-2 使用if-then-else语句

if command | condition

then

     commands 

else

     commands 

fi

#bin/bash
  
if ps -ef | grep mysql | grep -v grep
then
    echo "mysql is running"
else
    echo "mysql is stop"
fi

输出:会把ps -ef | grep mysql | grep -v grep

将ps -ef | grep mysql | grep -v grep输出重定向到垃圾篓。

#bin/bash
  
if ps -ef | grep mysql | grep -v grep &> /dev/null
then
    echo "mysql is running"
else
    echo "mysql is stop"
fi

输出:mysql is running

3-3 嵌套if

if command | condition

then

     commands 

elif command | condition

then 

     commands

else

     commands 

fi

#bin/bash
  
if ps -ef | grep mysql | grep -v grep &>/dev/null
then
    echo "mysql is running"
elif ps -ef | grep pycharm | grep -v grep &>/dev/null
then
    echo "pycharm is running"
else
    echo "mysql and pycharm is also  stop"
fi

输出:pycharm is running(停掉了mysql服务,打开了pycharm)

将then写在与if的同一行,then前面需要加;

#bin/bash
  
if ps -ef | grep mysql | grep -v grep &>/dev/null;then
    echo "mysql is running"
elif ps -ef | grep pycharm | grep -v grep &>/dev/null;then
    echo "pycharm is running"
else
    echo "mysql and pycharm is also  stop"
fi

输出:mysql and pycharm is also  stop(停掉了mysql服务和pycharm)

3-4 条件测试-数值比较

数值比较

  • n1 -eq n2    n1和n2相等,则返回true;否则返回false
  • n1 -ne n2    n1和n2不等,则返回true;否则返回false
  • n1 -gt n2     n1大于n2,则返回true;否则返回false
  • n1 -ge n2    n1大于等于n2,则返回true;否则返回false
  • n1 -lt n2       n1小于n2,则返回true;否则返回false
  • n1 -le n2      n1小于等于n2,则返回true;否则返回false  

注意,数值比较要用[]扩起来,而且[与第一个变量 以及]与最后一个变量都要有空格。

#!bin/bash
  
if [ $1 -eq $2 ]; then
    echo "$1 = $2"
elif [ $1 -gt  $2 ]; then
    echo "$1 >  $2"
elif [ $1 -lt  $2 ]; then
    echo "$1 <  $2"
fi

执行:sh if_condition.sh 12 12    输出:12 = 12

执行:sh if_condition.sh 12 13    输出:12 <  13

3-5 条件测试-字符串比较

字符串比较

  • s tr1 = str2 
  • str1 != str2
  • str1 < str2
  • str1 > str2
  • -n str1             str1长度不是0则为True
  • -z str1             str1长度是0则为True

注意:

1. 字符串比较,若[]单方括号,则最好将变量引起来(可以有效应对变量不存在或空值的情况),例如[ -n "$1" ],以及> 和 < 均需要转义(他们均是重定向的特殊符号,特殊符号需要转义),例如[ "$1" \>  "$2" ], [ "$1" \<  "$2" ]

2. 也可以用[[]]双方括号,则里面的变量可以不用引起来,以及< 和 > 也不需要转义

#!bin/bash
  
if [ $1 = $2 ];then
    echo "$1 = $2"
elif [ $1 >  $2 ];then
    echo $?
    echo "$1 > $2"
else
    echo "$1 < $2"
fi  

执行:sh if_condition_str.sh a b

输出:这边将>当作了重定向符号,也确实产生了文件b,同时可以看到退出状态码是0。

0
a > b

用sh -x来看执行过程,sh -x if_condition_str.sh a b,先判断a和b是否相等,不相等走到elif,elif运算结果是' a '为True。

+ '[' a = b ']'
+ '[' a ']'
+ echo 0
0
+ echo 'a > b'
a > b

这边我们注意<和>均是shell中的特殊符号,这边需要转义。

#!bin/bash
  
if [ "$1" = "$2" ];then  #将变量引起来能有效应对变量不存在、空值的情况  
    echo "$1 = $2"
elif [ "$1" \>  "$2" ];then   # > 是重定向符号,需要转义
    echo "$1 > $2"
else
    echo "$1 < $2"
fi

执行:sh if_condition_str.sh a b

输出:和高级语言中字符串比较一样。

a < b

执行:sh -x if_condition_str.sh a b

+ '[' a = b ']'
+ '[' a '>' b ']'
+ echo 'a < b'
a < b
#!/bin/bash

if [ -n "$1" ];then
   echo "len > 0"
else
   echo "len = 0"
fi

3-6 条件测试-文件比较

  • -d file      file是否为目录
  • -f file       file是否为文件
  • -e file      file是否存在
  • -r file       file是否可读
  • -w file      file是否可写
  • -x file       file是否可执行
  • -s file       file存在且非空
  • file1 -nt file2  file1比file2新为True
  • file1 -ot file2  file1比file2旧为True
#!/bin/bash

if [ -d /Users ]; then
    echo "yes"
else
    echo "no"
fi

查看当前目录下的文件 if_then_else.sh是否可读。

#!/bin/bash
  
if [ -r ./if_then_else.sh ]; then
    echo "yes"
else
    echo "no"
fi

3-7 复合条件测试

if condition1 && condition2

then

     commands 

fi

if condition1 || condition2

then

     commands 

fi

#!/bin/bash
  
v1=56
v2=34
v3=89

if [ $v1 -gt $v2 ] && [ $v2 -lt $v3 ]; then
    echo "$v1 > $v2 并且 $v2 < $v3"
fi

输出:56 > 34 并且 34 < 89

3-8 if-then中使用双括号

使用双括号进行算数运算

  • 使用双括号进行算数运算,可以写类C语言的表达式
  • a++ 或者 b-- 或者 a+=1 或者 a<b 或者 a!=b

if [ $v1 -lt $v2 ]

then

     commands 

fi

if ((expression))

then

     commands 

fi

可用运算符

  • value++    后增
  • value--     后减
  • ++value    先增
  • --value     先减
  • !                逻辑求反
  • ==            相等
  • >               大于
  • <                
  • >=
  • <=
  • &&            逻辑与
  • ||               逻辑或
  • +=

注意事项

  • 双括号结构,变量名引用可以加$,也可以不加
  • 运算符前后可以有空格,也可以没有
  • 可以用于if、for、while等循环控制结构中
  • 多个运算符使用逗号分隔。
#!/bin/bash

a=$1
b=$2
c=$3

if (( a > b && $c > b));then
    echo "$1>$2 且 $3>$2"
else
    echo "no"
fi

执行:sh run3-8.sh 29 9 89,输出:29>9 且 89>9 

#!/bin/bash

a=10
echo "a: $a"

(( b=a++ ))

echo "自增后a: $a"
echo "b: $b"

输出:

a: 10
自增后a: 11
b: 10 

3-8 if-then中使用双方括号

使用双方括号支持类C语言的条件测试

注意事项

  • 双括号结构中,变量名引用必须加$
  • [[ 后面必须有空格,]] 前面也必须有空格

在mac的环境下,这样用还是会不符合逻辑,但是好像有些linux环境下可以。还是按3-7的来吧。

#!/bin/bash

if [[ $1 > $2 && $3 > $2 ]];then
    echo "yes"
else
    echo "no"
fi

执行:sh double_squaare.sh 12 5 8 输出no

+ [[ 12 > 5 ]]
+ echo no
no
#!/bin/bash
  
if [[ $1 -gt $2 && $3 -gt $2 ]];then
    echo "yes"
else
    echo "no"
fi  

执行:sh double_squaare.sh 12 5 8 输出yes

+ [[ 12 -gt 5 ]]
+ [[ 8 -gt 5 ]]
+ echo yes
yes

3-10 case命令

case $var in

    pattern1) #  pattern1可以是常量值、字符串、数值、正则表达式、通配符

        commands;;

    pattern2)

        commands;;

esac

注意:要添加;; *是通配,当输入jack和mike外的字符串均走*。

#!/bin/bash

case $1 in 
    jack)
        echo "hello jack";;
    mike)
        echo "hello mike";;
    *)
        echo "hello everyone";;
esac

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值