美文网首页
shell 中的条件测试

shell 中的条件测试

作者: Michael_林 | 来源:发表于2018-01-11 22:16 被阅读12次

条件测试

判断条件是否符合要求,就需要测试机制来实现,shell 中可以使用test命令来测试条件是否符合要求.bash中的测试可分为:
(1) 数值比较测试
(2) 字符串测试
(3) 文件测试

常用的测试方法包括:
(1) test命令 test expression
(2) [ expression ]
(3) [[ expression ]]

test 测试命令的用法

用法: test [expr]

Evaluate conditional expression.
计算条件表达式  
Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR.  Expressions may be unary or binary.  Unary
expressions are often used to examine the status of a file.  There
are string operators as well, and numeric comparison operators.
退出状态为0(真)或1(假)取决于EXPR的测试。 表达式可能是一元或二元的。一元
表达式通常用于检查文件的状态。 也可以做字符串运算符以及数字比较运算符。

File operators文件测试操作:
    单文件测试:  
  -a FILE        True if file exists.  文件是否存在true/false
  -b FILE        True if file is block special.    判断是否是一个块设备文件  
  -c FILE        True if file is character special. 判断是否是一个字符文件  
  -d FILE        True if file is a directory.  判断是否是一个目录  
  -e FILE        True if file exists. 文件存在性测试  
  -f FILE        True if file exists and is a regular file. 文件是否存在并且是一个普通文件  
  -g FILE        True if file is set-group-id.  存在且设置了sgid
  -h FILE        True if file is a symbolic link.  存在且为链接文件  
  -L FILE        True if file is a symbolic link.  存在且为链接文件   
  -k FILE        True if file has its `sticky' bit set. 文件存在且有sticky权限  
  -p FILE        True if file is a named pipe.   是否存在且为命令管道文件  
  -r FILE        True if file is readable by you.  是否可读  
  -s FILE        True if file exists and is not empty.  是否存在且不为空  
  -S FILE        True if file is a socket.  是否是一个socket套接字文件  
  -t FD          True if FD is opened on a terminal.  文件的描述符是否在某个终端打开  
  -u FILE        True if the file is set-user-id.  存在且设置了suid  
  -w FILE        True if the file is writable by you.   当前用户是否有写权限
  -x FILE        True if the file is executable by you. 当前用户是否有写权限  
  -O FILE        True if the file is effectively owned by you. 文件的属主是否是当前用户  
  -G FILE        True if the file is effectively owned by your group. 文件的属组是否是当前组  
  -N FILE        True if the file has been modified since it was last read.  文件自从上一次被读取后是否被修改过  

双文件测试:  
测试文件的新旧
  FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                   modification date).file1是否比file2旧(修改时间戳)

  FILE1 -ot FILE2  True if file1 is older than file2. file1是否比file2新(修改时间戳)  

  FILE1 -ef FILE2  True if file1 is a hard link to file2.file2是否是file1的硬链接,或是是否为同一个文件(文件的inode是同一个) 。  

String operators字符串测试操作符:  
是否为空:  
  -z STRING      是否为空 True if string is empty.

  -n STRING      是否不空  
     STRING      True if string is not empty.
大小测试(按顺序比较anssic码) :  
  STRING1 = STRING2  相等  
                 True if the strings are equal. 
  STRING1 != STRING2 不等于  
                 True if the strings are not equal.
  STRING1 < STRING2  小于  
                 True if STRING1 sorts before STRING2 lexicographically.
  STRING1 > STRING2  大于  
                 True if STRING1 sorts after STRING2 lexicographically.
 

Other operators其他的测试  :

  -o OPTION      True if the shell option OPTION is enabled.
  ! EXPR         True if expr is false.
  EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
  EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
  数值大小测试:  
  arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                 -lt, -le, -gt, or -ge.
    -eq 等于  
    -ne 不等于  
    -lt 小于  
    -le 小于等于  
    -gt 大于 
    -ge 大于等于  

Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.

Exit Status:
Returns success if EXPR evaluates to true; fails if EXPR evaluates to
false or an invalid argument is given.

其他测试命令:

[ expr ] 表达式前后必须要有空格,否则错误
例如: [ 1 -gt 3 ] && echo "hello" || echo "no hello"
[[ expr ]]
例如: [[ 1 -gt 3 ]] && echo "hello" || echo "no hello"

以上测试命令可以使用同样的操作符

模式匹配测试

STRING1 =~ pattern 左侧的字符串能否被右侧的模式匹配到,只能在[[ ]] 中使用
例如: [[ "a.sh" =~ "*.sh" ]] && echo "yew" || echo "no"

注意

字符串的测试操作必须要带上双引号

自定义退出状态码

exit [n] 指定退出状态码
注意:
(1) 脚本中一旦遇到exit 命令,脚本会# 立即 #终止。
(2) 脚本的执行状态码是最后一条命令的退出状态码

组合条件测试

条件组合也叫逻辑运算, 逻辑运算包括或运算、与运算、取反(非)
使用方式:
(1) 使用运算符连接多个条件表达式
与: condition1 && condition2
或: condition1 || condition2
非: !condition
例如:
(1) [ -e r.pub ] && [ -r r.pub ] && echo "yes" || echo "no"
(2) [ -z "$HOSTNAME" ] && [ "$USER" != "root" ] && echo "you can't set hostname" || echo "you can set hostname"

(2)使用命令的参数方式 :
与: -a and condition1 -a condition2
或: -o or condition1 -o condition2
非: ! !condition
例如:
(1) test -e r.pub -a -r r.pub && echo "true" || echo "false"
(2)[ -n hostname -a hostname != "localhost.localdomain" ] && echo "yes" || echo "no"

相关文章

网友评论

      本文标题:shell 中的条件测试

      本文链接:https://www.haomeiwen.com/subject/lqxxoxtx.html