zoukankan      html  css  js  c++  java
  • 学习linux的菜鸟 shell脚本中的逻辑判断

    shell脚本中的逻辑判断

     if 逻辑判断。在shell中if判断的基本语法为:

    1)不带else

    if  判断语句; then
        command
    fi
    

    例如:

    [root@localhost sbin]# cat if1.sh
    #! /bin/bash
    
    read -p "Please input your score: " a
    if (($a<60)); then
        echo "You didn't pass the exam."
    fi

    在if1.sh中出现了 (($a<60)) 这样的形式,这是shell脚本中特有的格式,用一个小括号或者不用都会报错,

    请记住这个格式或者可以写为[ $a lt 60 ]。执行结果为:

    [root@localhost sbin]# sh if1.sh
    Please input your score: 90
    [root@localhost sbin]# sh if1.sh
    Please input your score: 33
    You didn't pass the exam.

    2)带有else

    if  判断语句  ; then
        command
    else
        command
    fi
    

    例如:

    [root@localhost sbin]# cat if2.sh
    #! /bin/bash
    
    read -p "Please input your score: " a
    if (($a<60)); then
         echo "You didn't pass the exam."
    else
         echo "Good! You passed the exam."
    fi
    

    执行结果:

    [root@localhost sbin]# sh if2.sh
    Please input your score: 80
    Good! You passed the exam.
    [root@localhost sbin]# sh if2.sh
    Please input your score: 25
    You didn't pass the exam.
    

    和上一例唯一区别的地方是,如果输入大于等于60的数字会有所提示。

    
    

    3)带有elif

    if  判断语句一  ; then
        command
    elif  判断语句二; then
        command
    else
        command
    fi
    

    例如:

    [root@localhost sbin]# cat if3.sh
    #! /bin/bash
    
     read -p "Please input your score: " a
     if (($a<60)); then
             echo "You didn't pass the exam."
     elif (($a>=60)) && (($a<85)); then
             echo "Good! You pass the exam."
     else
             echo "very good! Your socre is very high!"
     fi
    

    这里的 && 表示 “并且” 的意思,当然也可以使用 || 表示 “或者” 执行结果为:

    [root@localhost sbin]# sh if3.sh
    Please input your score: 90
    very good! Your socre is very high!
    [root@localhost sbin]# sh if3.sh
    Please input your score: 60
    Good! You pass the exam.
    

    以上只是简单的介绍了if语句的结构。在判断数值大小除了可以用 (( )) 的形式外,还可以使用 [ ] 但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。

    [root@localhost sbin]# a=10; if [ $a -lt 5 ]; then echo ok; fi
    [root@localhost sbin]# a=10; if [ $a -gt 5 ]; then echo ok; fi
    ok
    [root@localhost sbin]# a=10; if [ $a -ge 10 ]; then echo ok; fi
    ok
    [root@localhost sbin]# a=10; if [ $a -eq 10 ]; then echo ok; fi
    ok
    [root@localhost sbin]# a=10; if [ $a -ne 10 ]; then echo ok; fi
    

    再看看if中使用 && 和 ||的情况:

    [root@localhost sbin]# a=10; if [ $a -lt 1 ] || [ $a -gt 5 ]; then echo ok; fi
    ok
    [root@localhost sbin]# a=10; if [ $a -gt 1 ] || [ $a -lt 10 ]; then echo ok; fi
    ok

    shell 脚本中if还经常判断关于档案属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的也就几个选项:

    -e :判断文件或目录是否存在

    -d :判断是不是目录,并是否存在

    -f :判断是否是普通文件,并存在

    -r :判断文档是否有读权限

    -w :判断是否有写权限

    -x :判断是否可执行

    使用if判断时,具体格式为:

    if -e filename then

    例子:

    [root@localhost sbin]# if [ -d /home/ ]; then echo ok; fi
    ok
    [root@localhost sbin]# if [ -f /home/ ]; then echo ok; fi
    

    因为 /home/ 为目录为非文件,所以并不会显示 “ok” .

    root@localhost sbin]# if [ -f /root/test.txt ]; then echo ok; fi
    ok
    [root@localhost sbin]# if [ -r /root/test.txt ]; then echo ok; fi
    ok
    [root@localhost sbin]# if [ -w /root/test.txt ]; then echo ok; fi
    ok
    [root@localhost sbin]# if [ -x /root/test.txt ]; then echo ok; fi
    [root@localhost sbin]# if [ -e /root/test1.txt ]; then echo ok; fi

    在shell 脚本中,除了用if来判断逻辑外,还有一种常用的方式,那就是case了。具体格式为:

    case  变量  in
    value1)
              command
              ;;
    value2)
              command
              ;;
    value3)
              command
              ;;
    *)
              command
              ;;
    esac
    
    

    上面的结构中,不限制value的个数, * 则代表除了上面的value外的其他值。下面阿铭写一个判断输入数值是奇数或者偶数的脚本:

    [root@localhost sbin]# cat case.sh
    #! /bin/bash
    
    read -p "Input a number: " n
    a=$[$n%2]
    case $a in
    
        1)
            echo "The number is odd."
            ;;
        0)
            echo "The number is even."
            ;;
        *)
            echo "It's not a number!"
            ;;
    esac
    

    $a 的值或为1或为0,执行结果为:

    [root@localhost sbin]# sh case.sh
    Input a number: 100
    The number is even.
    [root@localhost sbin]# sh case.sh
    Input a number: 101
    The number is odd.
    

    case脚本常用于编写系统服务的启动脚本,例如/etc/init.d/iptables中就用到了,你不妨去查看一下



























  • 相关阅读:
    最大的错误就是没有把自己的软件开发事业当 作一桩生意来看待
    python:open/文件操作
    Python 之ConfigParser
    Python 之ConfigParser
    Python 之ConfigParser
    python的time模块使用
    python的time模块使用
    python的time模块使用
    国外、国内各大OJ
    国外、国内各大OJ
  • 原文地址:https://www.cnblogs.com/llx--20190411/p/15502880.html
Copyright © 2011-2022 走看看