zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    MD5消息摘要算法的那些事
    关系数据库设计范式介绍(第一范式,第二范式,第三范式)
    C# string byte数组转换解析
    c#中FTP上传下载
    CString/string 区别及其转化
    伟大的神器 pjax 在thinkphp中的应用
    js jquery 判断当前窗口的激活点
    widget 传参数问题
    常见适用的函数
    thinkphp 分页函数
  • 原文地址:https://www.cnblogs.com/lbg-database/p/10109954.html
Copyright © 2011-2022 走看看