shell脚本--分支、条件判断

在看选择判断结构之前,请务必先看一下数值比较文件测试

 

if....else...

#!/bin/bash
#文件名:test.sh

score=66
# //格式一
if [ $score -lt 60 ]
then
    echo "60以下"
elif [ $score -lt 70 ]
then 
    echo "60-70"
else
    echo "70以上"
fi

# 格式二
if [ $score -lt 60 ];then
    echo "60以下"
elif [ $score -lt 70 ];then 
    echo "60-70"
else
    echo "70以上"
fi

  运行结果:

ubuntu@ubuntu:~$ ./test.sh
60-70
60-70
ubuntu@ubuntu:~$ 

  

支持嵌套if...else....也和其他语言一样,需要注意的是,每一个选择判断分支都要结束(使用if的反写fi)。

#!/bin/bash
#文件名:test.sh

score=66

if [ $score -lt 60 ];then
    if [ $score -gt 55 ];then
        echo "55-60"
    elif [ $score -lt 55 ];then
        echo "小于55"
    fi
elif [ $score -lt 70 ];then 
    if [ $score -gt 65 ];then
        echo "65-70"
    elif [ $score -lt 60 ];then
        echo "60-65"
    fi
else
    echo "70以上"
fi

  运行结果:

ubuntu@ubuntu:~$ ./test.sh
65-70
ubuntu@ubuntu:~$ 

  需要注意的是,上面的每一行都是一条命令,如果想要将某几行写在一行,那么要在每一行之后加分号,分号必不可少。

  掌握以上的内容后,看一下这个例子:

#!/bin/bash
#文件名:test.sh

num=6
if [ $num%2 -eq 0 ]
then
    echo "yes"
else
    echo "no"
fi

 如果你认为这个脚本可以正常运行,会输出yes的话,那你肯定是被其他语言中的if判断给影响了,不信看一下结果:

ubuntu@ubuntu:~$ ./test.sh
./test.sh: line 6: [: 6%2: integer expression expected
no
ubuntu@ubuntu:~$ 

  错误信息很好理解,数学运算的表达式在这里计算的方法或者存在的形式是不对的,那么要怎么才行呢?

  也许你会说将数学计算的表达式使用$((  ))括起来就行了,比如下面:

#!/bin/bash
#文件名:test.sh

num=6

if [ $[ $num % 2 ] -eq 0 ]
#或者下面两种方法
#if [ $(( $num % 2 )) -eq 0 ]
#if [ `expr $num % 2`  -eq 0 ]
then
    echo "yes"
else
    echo "no"
fi

  运行:

ubuntu@ubuntu:~$ ./test.sh
yes
ubuntu@ubuntu:~$ 

  推荐使用$[ command ] 来进行整数计算

 

case

  以case开始,以esac结尾(case反着写),每一个switch不用谢break,默认满足其中一个就会break。

#!/bin/bash
#文件名:test.sh

read -p "please input number: " week

case $week in
    1 | 2 | 3 | 4 | 5 )
        echo "I'm working";;
    6 | 7)
        echo "I'm playing";;
    *)  #类似去其他语言中的default
        echo "I'm dying";;
esac

  运行:

ubuntu@ubuntu:~$ ./test.sh
please input number: 6
I'm playing
ubuntu@ubuntu:~$ ./test.sh
please input number: 1
I'm working
ubuntu@ubuntu:~$ ./test.sh
please input number:8
I'm dying
ubuntu@ubuntu:~$ 

  其中每一个匹配的部分可以是常量或者变量,需要注意的是匹配的部分后面必须有右括号,然后每一种情况都要使用两个分号分隔。 从上之下开始匹配,一旦匹配,则执行相应的命令,不再进行后续的匹配判断。   

  *)表示前面所有情况都不匹配时,才执行下面的操作,一般用在最后,和其他语言中的default相同。

  另外case的匹配模式中,如果有多个匹配之后的操作都相同时,可以使用|来将多个匹配模式写在一起,表示或者,只要满足其中一个 模式,就执行这条语句。

 

  在指定条件的地方,还可以是正则表达式

  当然,还有一种情况也可以达到这种类似的效果,如下:

#!/bin/bash
#文件名:test.sh

read -p "please input the key: " key
case $key in
    [a-z] | [A-Z])
        #注意上面一条命令中间使用了|
        echo "alpha";;
    [0-9])
        echo "number";;
    *)
        echo "other key"
esac

  运行:

ubuntu@ubuntu:~$ ./test.sh
please input the key: a
alpha
ubuntu@ubuntu:~$ ./test.sh
please input the key: 2
number
ubuntu@ubuntu:~$ ./test.sh
please input the key: !
other key
ubuntu@ubuntu:~$ 

  

  

转载于:https://www.cnblogs.com/-beyond/p/8262138.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值