if,case

作者: 素瑾汐 | 来源:发表于2017-07-03 22:06 被阅读0次

    if

    条件选择,选择执行,可嵌套

    单分支
          if 判断条件;then
                    条件为真的分支代码
          fi
    
    双分支
          if 判断条件;then
                    条件为真的分支代码
          else
                    条件为假的分支代码 
          fi
    
    多分支
          if 判断条件1;then
                    条件为真的分支代码
          elif 判断条件2;then
                    条件为真的分支代码
          elif 判断条件3;then
                    条件为真的分支代码
          else
                    以上条件都为假的分支代码
          fi
    

    逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

    例子:根据命令的退出状态来执行命令
    if ping -c1 -W2 station1 &> /dev/null; then
    echo 'Station1 is UP'
    elif grep "station1" ~/maintenance.txt &> /dev/null then
    echo 'Station1 is undergoing maintenance‘
    else
    echo 'Station1 is unexpectedly DOWN!' exit 1
    fi

    [root@centos SC]#vim age.sh
    #!/bin/bash
    ######输入一个数
    read -p "please input your age: " age
    ######判断输入的值是否为数字,如有不是,提示输入数字并退出
    [[ ! "$age" =~ ^[[:digit:]]+$ ]] && echo please input digital && exit 100
    ######对输入的数进行判断,从而执行相应命令
    if [ "$age" -le 18 ];then
            echo "you are baby" 
    elif [ "$age" -gt 18 -a "$age" -le 60 ];then
            echo you need work hard
    elif [ "$age" -le 80 ];then
            echo "you can enjoy life"
    else
            echo "you will be lucky"
    fi
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    "age.sh" 12L, 331C written                                                
    [root@centos SC]#chmod +x age.sh
    ######测试结果
    [root@centos SC]#./age.sh       
    please input your age: 2
    you are baby
    [root@centos SC]#./age.sh
    please input your age: 18
    you are baby
    [root@centos SC]#./age.sh
    please input your age: 19
    you need work hard
    [root@centos SC]#./age.sh
    please input your age: 65
    you can enjoy life
    [root@centos SC]#./age.sh
    please input your age: 85
    you will be lucky
    

    case

    case语句:条件判断

    case  变量引用 in
    PATH1)
                    分支1
                    ;;  
    PATH2)
                    分支2
                    ;;
    ...
    *)
                    默认分支
                    ;;
    esac
    

    case支持glob风格的通配符:

         *:任意长度任意字符
         ?:任意单个字符
         []:指定范围内的任意单个字符
         a|b:a或b
    

    例子:

    [root@centos SC]#vim yesorno.sh 
    #!/bin/bash
    #####提示输入,读取输入的值
    read -p "please input yes or no: " ans
    #####将输入转换为小写输出
    ans=`echo $ans |tr '[:upper:]' '[:lower:]'`
    #####进行判断,执行相应命令
    case $ans in
    y|ye|yes)
        echo this is yes
        ;;
    n|no)
        echo this is no
        ;;
    *)
        echo This is other information , please input yes or no
    esac
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~                                                                                           
    ~  
    ######测试结果                                                                                         
    [root@centos SC]#./yesorno.sh     
    please input yes or no: y
    this is yes
    [root@centos SC]#./yesorno.sh 
    please input yes or no: Ye
    this is yes
    [root@centos SC]#./yesorno.sh 
    please input yes or no: yeS
    this is yes
    [root@centos SC]#./yesorno.sh 
    please input yes or no: n
    this is no
    [root@centos SC]#./yesorno.sh 
    please input yes or no: No
    this is no
    [root@centos SC]#./yesorno.sh 
    please input yes or no: nO
    this is no
    [root@centos SC]#./yesorno.sh 
    please input yes or no: P
    This is other information , please input yes or no
    

    相关文章

      网友评论

          本文标题:if,case

          本文链接:https://www.haomeiwen.com/subject/oupzcxtx.html