zoukankan      html  css  js  c++  java
  • 【shell】shell编程(三)-if,select,case语句

      通过前两篇文章,我们掌握了shell的一些基本写法和变量的使用,以及基本数据类型的运算。那么,本次就将要学习shell的结构化命令了,也就是我们其它编程语言中的条件选择语句及循环语句。

      不过,在学习shell结构化命令的时候,我们又会发现它与其它编程的语言相比存在不小的区别。下面就开始看看吧:

    条件选择

    在条件选择语句中,主要包含以下几种写法:

    if-then语句

    if command
    then
        commands
    fi
     
    例如:






    吃瓜群众表示一脸懵比:if语句后面接的是命令,我们其它编程语言中,这儿都是接返回布尔值(true,false)的表达式。

    那么这到底是怎么回事呢?

      在shell脚本的if其实是根据紧跟后面的那个命令的退出状态码来判断是否执行then后面的语句的。

    关于退出状态码,你只需要记住:正常退出(命令执行正常)的状态码是0, 非正常退出的状态码不是0(有不少)。

    以上语句的语义为: 如果if后面的命令执行正常(状态码0),那么就执行then后面的语句。否则不执行。 fi代表if语句的结束。

    #!/bin/bash
    #这儿由于pwd是linux内置的命令,因此执行后会正常退出(状态码0),所以会执行then中的语句
    #如果此处替换为一个不存在的命令(例如: pw),那么就会非正常退出,不会执行then中的语句
    if pwd
    then
       echo 执行then里面的语句
    fi

    if-then还可以简写为;

    if command; then
        commands
    fi
     

    因此,以上代码还可以写成以下:

    #!/bin/bash
    if pwd; then
       echo 执行then里面的语句
    fi
     

    以上,如果我要判断处理异常退出(状态码非0)情况,该怎么办?

    别着急: else 来帮你。

    if-then-else语句

    if command
    then
        commands
    else
        commands
    fi
     

    与if-then语句相比,这回多了个else语句,else语句用来判断if后面的命令非正常退出的情况。

    #!/bin/bash
    if pwd
    then
        echo 正常退出
    else 
        echo 非正常退出
    fi   

    甚至,我们还可以变形写出更多的else:

    if else-if else语句

    if condition1
    then
        command1
    elif condition2 
    then 
        command2
    else
        commandN
    fi

      

    但是上面就只能根据退出状态码判断,不能写表达式,你还让我怎么写? 我各个编程语言直接吊打你!

    不要慌,客官,请接着往下看:

     

    • test命令([ ]与test命令等价,前后留空格)

      test命令用于if-then或者if-then-else语句中,主要用于判断列出的条件是否成立,如果成立,就会退出并返回退出状态码0,否则返回非0。

       这意味着我们可以通过test命令来写表达式命令了。不过,对于已习惯其它编程语言的程序猿们(没学过的除外),不要高兴得太早,前方有坑,至于是什么坑,待会儿就能看到。

    先看看test命令的基本用法吧:

      直接用:

    test condition

    结合if-then语句用

    if  test condition
    then
        commands
    fi

    结合if-then-else语句用

    if  test condition
    then
        commands
    else 
        commands    
    fi

    条件成立就执行then语句,否则else语句。

    test命令只能判断以下三类条件:

    • 数值比较
    • 字符串比较
    • 文件比较

    例如:if test用法:

    [rwwwwst@iz2ze46xiz ~]# cat wwww.sh
    if test $[5<3]
            then echo '5 less than 3'
    else 
        echo '5 grater than 3' fi

    运行结果:

    $ ./test.sh
    5 less than 3

    结果与我们想的不一样啊?

    1.  数值比较  最好使用(( 5<3 ))  两个圆括号,前后空格

    比较描述
    n1 -eq n2 判断n1是否等于n2
    n1 -ge n2 判断n1是否大于或等于n2
    n1 -gt n2 判断n1是否大于n2
    n1 -le n2 判断n1是否小于或等于n2
    n1 -lt n2 判断n1是否小于n2
    n1 -ne n2 判断n1是否不等于n2

    特别提醒: 以上表格不用你去记住,在命令行下面, 执行man test就能看到这些了。后面的对与另外两种比较的同理

    将上面改为如下:

    #!/bin/bash
    if test 5 -le 3
            then echo '5 less than 3'
    else
            echo '5 grater than 3'
    fi

    运行结果:

    $ ./test.sh
    5 grater than 3

    好好的标准的数学比较符号不能使用,难道非得写这种文本形式?是不是觉得很别扭?

    不着急,还有替代方案:

    使用双括号

    双括号命令允许你在比较过程中使用高级数学表达式。关键是使用双括号,咱就可以用数学比较符号啦(等于==, 大于>, 小于< 等等都能使用啦)。
    使用方法:

    (( expression ))

    注意:括号里面两边都需要有空格

     所以上面也可以改写为:

    #!/bin/bash
    if (( 5<3 ))
            then echo '5 less than 3'
    else
            echo '5 grater than 3'
    fi

    运行结果:

    $ ./test.sh
    5 grater than 3
     

    2.  字符串比较(最好使用[[ str <str2 ]]两个尖括号,前后空格

    比较描述
    str1 = str2 判断str1是否与str2相同
    str1 != str2 判断str1是否与str2不相同
    str1 < str2 判断str1是否比str2小(根据ASCII)
    str1 > str2 判断str1是否比str2大(根据ASCII)
    -n str1 判断str1的长度是否非0
    -z str1 判断str1的长度是否为0

    程序猿们,要骂的就尽情释放吧。我反正是骂了。

    test命令和测试表达式使用标准的数学比较符号来表示字符串比较,而用文本代码来表示数值比较。这与其它语言相比都不一样。

     例如:

    (1)比较字符串相等的例子:

    #!/bin/bash
    if test test1=test1
       then
            echo "same"
    else
            echo "not same"
    fi
     结果:
    $ ./test.sh
    same
    (2)比较字符串小于的例子:
    #!/bin/bash
    if test test1<Test1
       then
            echo "test less than Test1"
    else
            echo "not less"
    fi
    结果:(sh的时候将<当成一个重定向的符号,我们可以将<转义进行比较)
    $ ./test.sh
    ./test.sh: line 2: Test1: No such file or directory
    not less

    改进:
    #!/bin/bash
    if test test1<Test1
       then
            echo "test less than Test1"
    else
            echo "not less"
    fi
    结果:
    $ ./test.sh
    test less than Test1

    注意,在使用大于(>)或小于(<)符号时,需要转义(>)(<),不然会把这两种符号时别为重定向(后面文章才会讲到)。

    吐槽模式开启:我要用个比较符号,还要转义,很蛋疼的设计!

    不要慌,大招一般都在后面:

    使用双方括号

    双方括号命令提供了针对字符串比较的高级特性。它不仅解决了使用test所带来的一系列毛病,还提供了一些test命令所没有的高级用法。双方括号命令的格式如下:

    [[ expression ]]    

    注意,可能有些shell不支持此种写法。不过bash完美支持。以上写法注意括号内两边都有空格。

    #!/bin/bash
    var1=test
    var2=Test
    if [[ $test < $test2 ]]
    then
        echo "test1 < test2"
    else
        echo "test1 >= test2"
    fi   

    这下终于不用转义了。

    因此上面也可以改为:

    #!/bin/bash
    if [[ test1<Test1 ]]
       then
            echo "test less than Test1"
    else
            echo "not less"
    fi

    结果:

    $ ./test.sh
    test less than Test1

    文件比较

    参数说明
    -e 文件名 如果文件存在则为真
    -r 文件名 如果文件存在且可读则为真
    -w 文件名 如果文件存在且可写则为真
    -x 文件名 如果文件存在且可执行则为真
    -s 文件名 如果文件存在且至少有一个字符则为真
    -d 文件名 如果文件存在且为目录则为真
    -f 文件名 如果文件存在且为普通文件则为真
    -c 文件名 如果文件存在且为字符型特殊文件则为真
    -b 文件名 如果文件存在且为块特殊文件则为真

    另外,Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。例如:

    cd /bin
    if test -e ./notFile -o -e ./bash
    then
        echo '至少有一个文件存在!'
    else
        echo '两个文件都不存在'
    fi
     

    例如:

    cd /bin
    if test -e ./bash
    then
        echo '文件已存在!'
    else
        echo '文件不存在!'
    fi
    •   select语句

    select 变量 in 关键字
        do 
            command1
            ...
            command 2
        done

      select把关键字的每一项做成类似表单,以交互的方式执行do和done之间的命。

    例如:

    #!/bin/bash
    #select Useage
    selectCh(){
            echo "what is you favorite foods?"
            select foos in "jidan" "dami" "fruit" "shi"
            do
                    if  [ "shi" = "$foos" ]
                            then
                            echo "you can not select shi?please select again!"
                            selectCh
                            exit 1
                    else
                            echo "you choice is ${foos}"
                            break
                            exit 1
                    fi
            done
    }
    selectCh

    运行结果:

    [root@VM_0_12_centos sshDemo]# ./testSelect.sh
    what is you favorite foods?
    1) jidan
    2) dami
    3) fruit
    4) shi
    #? 4
    you can not select shi?please select again!
    what is you favorite foods?
    1) jidan
    2) dami
    3) fruit
    4) shi
    #? 3
    you choice is fruit
    • case语句

    在使用if-then-else语句中,如果碰到条件很多的情况,如下:

    #!/bin/bash
    num=3
    if (( $num == 1 ))
    then
        echo "num=1"
    elif (( $num == 2 ))
    then
        echo "num=2"
    elif (( $num == 3 ))
    then
        echo "num=3"    
    elif (( $num == 4 ))
    then
        echo "num=4"
    fi 

    如果再多点条件,看起来是不是很多?
    此时,其实还有一种替代方案,那就是使用case.

    case variable in
    pattern1 | pattern2) 
      commands1;;
    pattern3)
      commands2;;
    *)
      default commands;; esac

     ;; 连续分号 (Terminator) 
      专用在 case 的选项,担任 Terminator 的角色。 
      case "$fop" inhelp) echo "Usage: Command -help -version filename";;version) echo "version 0.1" ;;esac

     

    将以上代码替换为case:

    #!/bin/bash
    num=3
    case $num in 1) echo "num=1";; 2) echo "num=2";; 3) echo "num=3";; 4) echo "num=4";; *) echo "defaul";; esac

     

    例如:一个启动tomcat的脚本:

    #!/bin/sh  
    # chkconfig: 2345 99 10  
    # description: Auto-starts tomcat  
    # /etc/init.d/tomcatd  
    # Tomcat auto-start  
    # Source function library.  
    #. /etc/init.d/functions  
    # source networking configuration.  
    #. /etc/sysconfig/network  
    RETVAL=0  
    export JAVA_HOME=/usr/java/jdk1.7.0_60  
    export JRE_HOME=/usr/java/jdk1.7.0_60/jre  
    export CATALINA_HOME=/usr/local/tomcat  
    export CATALINA_BASE=/usr/local/tomcat  
    start()  
    {  
            if [ -f $CATALINA_HOME/bin/startup.sh ];  
              then  
                echo $"Starting Tomcat"  
                    $CATALINA_HOME/bin/startup.sh  
                RETVAL=$?  
                echo " OK"  
                return $RETVAL  
            fi  
    }  
    stop()  
    {  
            if [ -f $CATALINA_HOME/bin/shutdown.sh ];  
              then  
                echo $"Stopping Tomcat"  
                    $CATALINA_HOME/bin/shutdown.sh  
                RETVAL=$?  
                sleep 1  
                ps -fwwu root | grep tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9  
                echo " OK"  
                # [ $RETVAL -eq 0 ] && rm -f /var/lock/...  
                return $RETVAL  
            fi  
    }  
      
    case "$1" in  
     start)   
            start  
            ;;  
     stop)    
            stop  
            ;;  
                                                      
     restart)  
             echo $"Restaring Tomcat"  
             $0 stop  
             sleep 1  
             $0 start  
             ;;  
     *)  
            echo $"Usage: $0 {start|stop|restart}"  
            exit 1  
            ;;  
    esac  
    exit $RETVAL 

    最后附三个简单的例子:

    (0)字符串比较是例子:

    #!/bin/bash
    #string complare example
    echo "please input the first string"
    read str1
    echo "please input the second string"
    read str2
    if [ ${str1} = ${str2} ]
            then
            echo "${str1} = ${str2}"
    elif [ ${str1} < ${str2} ]
            then
            echo "${str1} < ${str2}"
    else
            echo "${str1} > ${str2}"
    fi

    结果:

    [root@VM_0_12_centos sshDemo]# ./testString.sh
    please input the first string
    a
    please input the second string
    b
    a < b
    [root@VM_0_12_centos sshDemo]# ./testString.sh
    please input the first string
    a
    please input the second string
    a
    a = a

    (1)判断apache服务是否开启,如果没开启就打开apache服务:(字符串比较)

    #!/bin/bash
    web=`/usr/bin/pgrep httpd`
    #start detect apache Server
    if [ "$web" != "" ]
            then
            echo "apacheserver is running!"
    else
            echo "apache server is not running;"
            `which httpd`
    fi

    (2)比较两个数大小的例子(数字比较)

    #!/bin/bash
    if [ "$#" -eq 2 ]
            then
            echo "please input two number"
    else
            if [ "$1" -lt "${2}" ]
                    then
                    echo "$1 is less than ${2}"
            else
                    echo "$1 is equal or grater than $2"
            fi
    fi

    结果:

    [root@VM_0_12_centos sshDemo]# ./testNum.sh 2 3
    2 is less than 3

    (3)文件测试:

    #!/bin/bash
    if [ -f "$1" ]
            then
            echo "file is a plain file!"
            if [ -u "$1" ]
                    then
                    echo "file has SUID!"
            else
                    echo "file not has SUID!"
            fi
    
    elif [ -d "$1" ]
            then
            echo "file is a directory!"
    else
            echo "file is not in this system!"
    fi

    运行结果:

    [root@VM_0_12_centos sshDemo]# ./testFile.sh testApache.sh
    file is a plain file!
    file not has SUID!

    加上SUID位之后查看运行效果:

    [root@VM_0_12_centos sshDemo]# chmod u+s testApache.sh
    [root@VM_0_12_centos sshDemo]# ./testFile.sh testApache.sh
    file is a plain file!
    file has SUID!

    总结

       (1):取变量的时候最好${x}加上{}号,$#代表取参数的个数。

       (2)简单的比较大小的test用一个尖括号即可,如果是逻辑运算符用两个尖括号。

       (3)字符串比较大小用[]的时候用=,!=这些符号,而且用<,>符号的时候要转义<;另外[]前后要有空格,比较符前后也必须有空格。用[[]]比较字符串不用转义。

             (4)数字比较大小用[]的时候用-lt,-gt等这些符号,另外[]前后要有空格,比较符前后也必须有空格;如果使用两个圆括号数字比较也可以使用>,<这些,而且不用转义;用(())也可以对数字进行计算。

    小结

      本篇主要讲了条件语句。shell中的条件语句与其他编程语言相比有不小的区别,最大的区别就在于条件语句后接的是命令,而不是布尔值, 是根据命令执行退出的状态码来决定是否进入then语句的。这点需要牢记。

      接下来可以学习shell编程四:http://www.cnblogs.com/qlqwjy/p/7746001.html

      关于括号的用法参考:http://www.cnblogs.com/qlqwjy/p/8684630.html 

  • 相关阅读:
    Nginx入门使用
    下载pcre-8.35.tar.gz
    后端传到前端时间问题
    Layui弹出层分割线
    CSS动画实例:图文切换
    JavaScript小游戏实例:简单的键盘练习
    JavaScript小游戏实例:统一着色
    JavaScript动画实例:炸开的小球
    JavaScript动画实例:烟花绽放迎新年
    JavaScript动画实例:圆点的衍生
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/7745983.html
Copyright © 2011-2022 走看看