zoukankan      html  css  js  c++  java
  • Shell之条件测试

    一、什么是条件测试

    在bash的各种流程控制结构中通常需要进行各种测试,然后根据不同的测试结果执行不同的操作,比如常见的与if条件进行结合。

    语法:

             格式一:test<测试表达式>

             格式二:[<测试表达式>]

        格式三:[[<测试表达式>]]

    其中,格式一语格式二是等价的;格式三是扩展的test命令,在[[ ]]中可以使用通配符进行模式匹配、&&、||、>、<等操作符,但是[ ]中不可行。

     下面举例说明上面格式一、二、三的用法:

    1、实例一

    [root@localhost scripts]# test -f file && echo true||echo false 
    false
    [root@localhost scripts]# touch file
    [root@localhost scripts]# test -f file && echo true||echo false 
    true

    2、实例二

    [root@localhost scripts]# args=
    [root@localhost scripts]# test -n "$args" && echo 1 || echo 0
    0
    [root@localhost scripts]# test ! -z "$args" && echo 1 || echo 0
    0

    3、实例三

    [root@localhost scripts]# [ -f file1 ] && echo true||echo false 
    false
    [root@localhost scripts]# touch file1
    [root@localhost scripts]# [ -f file1 ] && echo true||echo false 
    true

    4、实例四

    [root@localhost scripts]# [[ -f file1 && -f file ]] && echo true||echo false  
    true

    二、文件测试操作符 

    文件测试操作符 说明
     -f file  若文件存在且为普通文件则为真
     -d directory  若文件存在其为目录则为真
     -s size  若文件存在且不为空(文件大小非0)则为真
     -e exist  若文件存在且为普通文件则为真
     -r read  若文件存在且可读则为真
     -w write  若文件存在且可写则为真
     -x excute  若文件存在且可执行则为真
     -L link  若文件存在且为链接文件则为真
     f1 -nt f2  newer than  若文件f1比文件f2新则为真
     f1 -ot f2 older than   若文件f1比文件f2旧则为真

    实例:

    [root@localhost scripts]# [ -f file2 ] && echo true||echo false 
    false
    [root@localhost scripts]# [ -e file2 ] && echo true||echo false 
    false
    [root@localhost scripts]# touch file2
    [root@localhost scripts]# [ -r file2 ] && echo true||echo false 
    true
    [root@localhost scripts]# [ -w file2 ] && echo true||echo false 
    true
    [root@localhost scripts]# [ -x file2 ] && echo true||echo false 
    false
    [root@localhost scripts]# [ -s file2 ] && echo true||echo false 
    false

    三、字符串测试操作符

    字符串测试操作符的作用:

    • 比较两个字符串是否相同
    • 判断字符串长度是否为0
    • 判断字符串是否为null
      字符串测试操作符 说明
      -z "字符串" 若字符串长度为0则为真,-z zero
      -n "字符串" 若字符串长度不为0则为真,-n no zero
      "字符串1"==“字符串2” 若字符串1等于字符串2则为真
      "字符串1"!=“字符串2” 若字符串1不等于字符串2则为真

    注意,字符串测试操作符后的字符串或者字符串变量使用“”括起来,避免出错。

    [root@localhost scripts]# args=""
    [root@localhost scripts]# [ -z "$args" ] && echo true || echo false
    true
    [root@localhost scripts]# [ -n "$args" ] && echo true || echo false
    false
    
    [root@localhost scripts]# args1=
    [root@localhost scripts]# [ -z "$args1" ] && echo true || echo false
    true
    [root@localhost scripts]# [ -n "$args1" ] && echo true || echo false
    false

    四、整数二元比较操作符

    []中使用的比较符 (())、[[]]中使用的比较符 说明
    -eq == 相等,equal
    -ne != 不想等,not equal
    -gt > 大于,greater than
    -ge >= 大于等于,greater equal
    -lt < 小于,less than
    -le <= 小于等于,less equal

    实例:

    [root@localhost scripts]# [ 2 -lt 3 ] && echo true || echo false
    true
    [root@localhost scripts]# [ 2 -gt 3 ] && echo true || echo false
    false
    [root@localhost scripts]# [ 2 -ge 3 ] && echo true || echo false
    false
    
    [root@localhost scripts]# [[ 2 < 3 ]] && echo true || echo false
    true
    [root@localhost scripts]# [[ 2 > 3 ]] && echo true || echo false
    false
    [root@localhost scripts]# [[ 2 != 3 ]] && echo true || echo false
    true

    五、逻辑操作符 

    []中使用的逻辑操作符 [[]]中使用的逻辑操作符 说明
    -a && 两端都为真,则为真,and
    -0 || 两个有一个为真,则为真,or
    ! ! 相反则为真,not

     实例:

    [root@localhost scripts]# [ -f file -a -file1 ] && echo true || echo false
    true
    [root@localhost scripts]# [ -f file -a -f file1 ] && echo true || echo false
    true
    [root@localhost scripts]# [ -f file -o -f file1 ] && echo true || echo false
    true
    [root@localhost scripts]# [ -f file -o ! -f file1 ] && echo true || echo false
    true
    [root@localhost scripts]# [ -f file -a ! -f file1 ] && echo true || echo false
    false

    六、其它

     1、简化条件语句

    在上面的shell语句中,使用:

    [ -f file -o -f file1 ] && echo true || echo false

    实际它等效于:

    if [ -f file -o -f file1 ]; then echo true; else echo false;fi

    使用上面的语法可以达到简化的效果。如果&&或者||后面有多个语句使用大括号扩起来,每条语句使用分号隔离。

    2、学习系统shell

    系统中有很多shell,可以学习优秀的写法,比如:/etc/init.d/network等文件。

    作者:iveBoy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Centos 安装字体库 以及解决confluence 旧文档数据的乱码
    设置Linux自启服务以及优先级
    Centos7下设置MySql自动启动
    Linux配置tomcat开机自启
    Linux安装JDK、tomcat
    CentOS7下安装mysql5.1 或升级到5.7 以及小问题的解决方案
    安装虚拟机,镜像文件下载链接
    1.键盘消失,阻止键盘弹出
    5. react父子组件
    CSS- 一些少用的
  • 原文地址:https://www.cnblogs.com/shenjianping/p/14351881.html
Copyright © 2011-2022 走看看