zoukankan      html  css  js  c++  java
  • 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:~$ 
    

      

      

  • 相关阅读:
    3种方法实现CSS隐藏滚动条并可以滚动内容
    javascript 计算两个整数的百分比值
    使用watch监听路由变化和watch监听对象的实例
    springboot全局捕获异常
    使用 Java 创建聊天客户端-2
    使用 Java 创建聊天客户端-1
    使用 ServerSocket 建立聊天服务器-2
    使用 ServerSocket 建立聊天服务器-1
    ServerSocket
    scheduled定时任务+实例请求数据库
  • 原文地址:https://www.cnblogs.com/-beyond/p/8262138.html
Copyright © 2011-2022 走看看