zoukankan      html  css  js  c++  java
  • 4、shell中的test命令

    bash/pdksh中,命令test用于计算一个条件表达式的值.他们经常在条件语句和循环
    语句中被用来判断某些条件是否满足.
    test命令的语法格式:
    test expression
    或者
    [expression]
    test命令中,可以使用很多shell的内部操作符.这些操作符介绍如下:
    (1)字符串操作符用于计算字符串表达式
    test命令 | 含义
    -----------------------------------------
    Str1 = str2 | 当str1与str2相同时,返回True
    Str1! = str2| 当str1与str2不同时,返回True
    Str | 当str不是空字符时,返回True
    -n str | 当str的长度大于0时,返回True
    -z str | 当str的长度是0时,返回True
    -----------------------------------------
    (2)整数操作符具有和字符操作符类似的功能.只是他们的操作是针对整数
    test表达式 | 含义
    ---------------------------------------------
    Int1 -eq int2|当int1等于int2时,返回True
    Int1 -ge int2|当int1大于/等于int2时,返回True
    Int1 -le int2|当int1小于/等于int2时,返回True
    Int1 -gt int2|当int1大于int2时,返回True
    Int1 -ne int2|当int1不等于int2时,返回True
    -----------------------------------------
    (3)用于文件操作的操作符,他们能检查:文件是否存在,文件类型等
    test表达式 | 含义
    ------------------------------------------------
    -d file |当file是一个目录时,返回 True
    -f file |当file是一个普通文件时,返回 True
    -r file |当file是一个刻读文件时,返回 True
    -s file |当file文件长度大于0时,返回 True
    -w file |当file是一个可写文件时,返回 True
    -x file |当file是一个可执行文件时,返回 True
    ------------------------------------------------
    (4)shell的逻辑操作符用于修饰/连接包含整数,字符串,文件操作符的表达式
    test表达式 | 含义
    ----------------------------------------------------------
    ! expr |当expr的值是False时,返回True
    Expr1 -a expr2|当expr1,expr2值同为True时,返回True
    Expr1 -o expr2|当expr1,expr2的值至少有一个为True时,返回True
    -----------------------------------------------------------
    注意:
    tcsh shell 不使用test命令,但是tcsh中的表达式同样能承担相同的功能.tcsh
    支持的表达式于C中的表达式相同.通常使用在if和while命令中.
    tcsh表达式 | 含义
    -------------------------------------------------------
    Int1 <= int2 |当int1小于/等于int2时,返回True
    Int1 >= int2 |当int1大于/等于int2时,返回True
    Int1 < int2 |当int1小于int2时,返回True
    Int1 > int2 |当int1大于int2时,返回True
    Str1 == str2 |当str1与str2相同时,返回True
    Str1 != str2 |当str1与str2不同时,返回True
    -r file |当file是一个可读文件时,返回True
    -w file |当file是一个可写文件时,返回True
    -x file |当file是一个可执行文件时,返回True
    -e file |当file存在时,返回True
    -o file |当file文件的所有者是当前用户时,返回True
    -z file |当file长度为0时,返回True
    -f file |当file是一个普通文件时,返回True
    -d file |当file是一个目录时,返回True
    Exp1 || exp2 |当exp1和exp2的值至少一个为True时,返回True
    Exp1 && exp2 |当exp1和exp2的值同为True时,返回True
    ! exp |当exp的值为False时,返回True

  • 相关阅读:
    Java 8 Lambda 表达式
    OSGi 系列(十二)之 Http Service
    OSGi 系列(十三)之 Configuration Admin Service
    OSGi 系列(十四)之 Event Admin Service
    OSGi 系列(十六)之 JDBC Service
    OSGi 系列(十)之 Blueprint
    OSGi 系列(七)之服务的监听、跟踪、声明等
    OSGi 系列(六)之服务的使用
    OSGi 系列(三)之 bundle 事件监听
    OSGi 系列(三)之 bundle 详解
  • 原文地址:https://www.cnblogs.com/quyong/p/6688001.html
Copyright © 2011-2022 走看看