zoukankan      html  css  js  c++  java
  • Linux Shell编程(1): 条件语句

    1.if—then
    #!/bin/bash
    if date              如果命令运行成功(退出码为0),则then部分的命令被执行
    then
       echo "good"
    fi

    2.if—then—else
    #!/bin/bash
    if hunter
    then
       echo "good"
    else 
       echo "bad"        if语句中退出码非0,则执行else部分语句
    fi

    3.elif
    #!/bin/bash
    if hunter
    then
       echo "command not found"
    elif date            多重If判断
    then
       echo "date"
    fi

    4.test命令           与其它编程语言思维一样的方式
    #!/bin/bash
    if test 1 -eq 2      test命令列出条件成立则退出并返回状态码0,命令等同if [ 1 -eq 2]
    then
       echo "right"
    else
       echo "wrong"
    fi

    5.条件比较
    1)数据比较
    n1 -eq n2            相等
    n1 -ge n2            大于或等于
    n1 -gt n2             大于
    n1 -le n2             小于等于
    n1 -lt n2              小于
    n1 -ne n2            不等于
    2)字符串比较
    str1 = str2
    str1 != str2
    str1 < str2
    str1 > str2
    -n str1              检查str1的长度是否非0
    -z str1              检查str1的长度是否为0

    #!/bin/bash
    str1=good
    str2=wrong
    if [ $str1 > $str2 ]          大于号需要转义,否则脚本会当重定向符号
    then echo "$str1 is greater than $str2"
    else
       echo "$str1 is less than $str2"
    fi

    #!/bin/bash
    str=
    if [ -z "$str" ]               判断字符串是否为空,空的或未初始化的变量对Shell脚本很致命
    then 
       echo "empty"
    else
       echo "not empty"
    fi

    4)复合条件&&与、||或
    #!/bin/bash
    if [ -e str ] && [ -f zero ]
    then
       echo "file all exist"
    fi

    5)双圆括号
    #!/bin/bash
    str1=good
    str2=wrong
    if (( $str1 > $str2 ))        双圆括号中表达式里的大于号不用转义
    then echo "$str1 is greater than $str2"
    else
       echo "$str1 is less than $str2"
    fi

    6)双方括号
    #!/bin/bash

    if [[ $USER == r* ]]          支持通配符模式
    then
       echo "welcome root user"
    else
       echo "sorry"
    fi
    

      



    7)case分支
    #!/bin/bash
    case $USER in
    root|hunterno4)
       echo "welcome root user";;           尾部两个分号
    mysql)
       echo "welcome to database";;
    surfftp)
       echo "nice to meet you";;
    *)                                      都不匹配时的default语句
       echo "i don't know you";;
    esac

    #! /bin/bash
    
    str1=10
    str2=10
    if [ $str1 -gt $str2 ]          #大于号需要转义,否则脚本会当重定向符号
    then echo "$str1 is greater than $str2"
    elif [ $str1 -eq $str2 ]
    then echo "$str1 is equal to $str2"
    else
       echo "$str1 is less than $str2"
    fi
    
    str="abc"
    if [ -z $str ]
    then
        echo "empty"
    else
       echo "not empty, the string is: "$str
    fi
    
    str3="aaa"
    if [[ $str3="aaa" ]]
    then echo "str3($str3) equals "aaa""
    else
    echo "not equals"
    fi
    
    
    if test 1=1
    then echo "test "test" "
    else echo "not equals"
    fi
  • 相关阅读:
    用R语言实现对不平衡数据的四种处理方法
    用R语言实现对不平衡数据的四种处理方法
    Java学习——Applet画8个同心圆
    Java学习——Applet画8个同心圆
    Java学习——Applet写字符串(调字体)
    Java学习——Applet写字符串(调字体)
    ZOJ 2913 Bus Pass (近期的最远BFS HDU2377)
    机房结账功能分析
    数据结构记录--排序
    C#创建PDF文档
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/4792834.html
Copyright © 2011-2022 走看看