• shell中数字、字符串、文件比较测试


    1.逻辑运算符:与&&     或||    非!

     &&:双目操作符:与运算中:如果第一个数为假,结果一定为假   ==> 短路操作符  

    ||:双目操作符:或运算中:如果第一个数为真,结果一定为真    ==> 短路操作符

    !:单目操作符:  对数取反.

    例子:

     [root@lbg test]# echo 2 && echo 3
    2
    3
    [root@lbg test]# echo 2 ||  echo 3 
    2
    [root@lbg test]# 

    2.测试表达式
     [ 表达式 ]  ---比较数字大小 ,表达式与中括号两端必须有空格
     [[ 表达式 ]]  --比较字符大小,表达式与中括号两端必须有空格。
     
     
    3.算术比较运算符:
      num1 -eq num2         //测试num1是否等于num2  (eq:equal)

        -ne                //测试num1是否不等于num2

        -gt             //大于 great than

        -lt             //小于 less than

        -ge             //大于等于 great  equal

        -le             //小于等于 less equal

    例子:

    [root@lbg test]# [ 1 -lt 2 ]  && echo yes || echo no
    yes
    [root@lbg test]# [ 1 -gt 2 ]  && echo yes || echo no
    no
    4.字符串测试
     'string1' == 'string2'                    //做等值比较

    !=,<>              //不等值比较

    -n "$a"           //跟变量名,a变量的值的长度非0为真

    -z "$a"         //测试是否为空的,空为真,非空为假,即值得长度为0为真

    例子:
    [root@lbg test]# [[ a == b ]] && echo yes || echo no
    no
    [root@lbg test]# [[ a != b ]] && echo yes || echo no
    yes
    [root@lbg test]# echo $a
    123
    [root@lbg test]# [[ -n $a ]] && echo yes || echo no
    yes
    [root@lbg test]# declare b      ---$b为空
    [root@lbg test]# [[ -n $b ]] && echo yes || echo no
    no
    [root@lbg test]# [[ -z $b ]] && echo yes || echo no
    yes
    5.文件测试
     -e        /path/to/somewhere    是否存在,存在为真    --exist

    -f   /path/to/somewhere  是否是文件,是则为真      --file

    -d          //测试是否是目录,是为真          --directory

    -l          //测试是否是链接,是为真         --link

    -r          //是否可读               --read

    -w          //是否可写              --write

    -x          //是否可执行             --execute

    说明:文件测试用单个[]或者[[]]都行。

    例子:
    [root@lbg test]# ll                    ----只有普通文件a
    -rw-r--r-- 1 root root 37 Oct  5 19:13 a
    [root@lbg test]# [[ -e /test/a ]] && echo yes || echo no
    yes
    [root@lbg test]# [[ -e /test/b ]] && echo yes || echo no
    no
    [root@lbg test]# [[ -d /test/a ]] && echo yes || echo no
    no
    [root@lbg test]# [ -x /test/a ] && echo yes || echo no  
    no

    6.组合条件测试
     !                     //取反,写在表达式之前,且用空格隔开

    -a      //与条件,写在[]里面,表示and

    -o      //或条件,写在[]里面,表示or

      例子:
    [root@lbg test]# [  d == f  ] && echo yes || echo no
    no 
    [root@lbg test]# [ ! d == f  ] && echo yes || echo no 
    yes
    [root@lbg test]# [ ! d == f  ] && [ a == b ]&& echo yes || echo no
    no
    [root@lbg test]# [ ! d == f -a c == b ]  && echo yes || echo no
    no
    [root@lbg test]# [ ! d == f -o c == b ]  && echo yes || echo no
    yes
  • 相关阅读:
    Gridview如何用自定义按钮进行编辑和提交修改
    winform多线程中给datagridview绑定数据源
    DevExpress控件WebchartControl的学习记录
    datagridview右键选中单元格并获取到焦点
    asp.net局部页面打印,以及如何去掉打印时自动保留的URL地址(页眉页脚)
    GridView如何实现点击某行的指定列弹出新窗体
    C# Color Font 与String之间的转换
    推荐一款 asp.net js日历控件
    js浮点运算替代函数
    VSeWss 1.3 CTP 安装后出现错误
  • 原文地址:https://www.cnblogs.com/lbg-database/p/10109954.html
Copyright © 2020-2023  润新知