美文网首页
06 - shell条件判断

06 - shell条件判断

作者: 舍是境界 | 来源:发表于2022-03-18 07:04 被阅读0次

shell脚本的智能化

  • 使shell脚本获得识别能力
  • 为命令的执行提供最直接的识别依据
    • 文件或目录的读/写等状态
    • 数值的大小
    • 字符串是否匹配
    • 多条件组合

test测试操作

  • 语法格式
    • test 选项 参数
    • [选项 参数]
help test
test: test [expr]
    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.
    
    File operators:
    
        -a FILE        True if file exists.
        -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.
        -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.
        -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.
        -t FD          True if FD is opened on a terminal.
        -u FILE        True if the file is set-user-id.
        -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 -ot FILE2  True if file1 is older than file2.
    
      FILE1 -ef FILE2  True if file1 is a hard link to file2.
    
    String operators:
    
        -z STRING      True if string is empty.
    
        -n STRING
        STRING         True if string is not empty.
    
        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.

字符串比较

  • 基本语法
    • 是否为空:[ -z 字符串]
    • 等于: [ 字符串1 == 字符串2 ]
    • 不等于:[ 字符串1 != 字符串2 ]
[ -z "$TT" ]
echo $?
0

[ -z "$x" ]
echo $?
1

[ "$USER" == root ]
echo $?
1

 [ a == a ]
echo $?
0

[ b == a ]
echo $?
1

整数值比较

  • [ 整数值1 操作符 整数值2 ]
操作符 含义
-eq 等于(Equal)
-ne 不等于(not equal)
-ge 大于或等于(greater or equal)
-le 小于或等于(less or equal)
-gt 大于(greater than)
-lt 小于(less than)
who | wc -l
       2
$ [ $(who |wc -l) -le 5 ]
echo $?
0

[ 3 -lt 8 ];echo $?
0

$ [ 3 -lt 2 ];echo $?
1

文件状态测试

  • [ 操作符 文件或目录 ]
操作符 含义
-e 判断对象是否存在(exist),若存在则为真
-d 判断是否为目录(directory),是则为真
-f 判断对象是否为一般文件(File),是则为真
-r 判断对象是否有可读(read)权限,是则为真
-w True if the file is writable by you.
-x True if the file is executable by you.
[ -e /etc/ ]; echo $?
0

[ -e /etc/hosts ]; echo $?
0

 [ -e /etc/xxx ]; echo $?
1

[ -r /etc/ ]; echo $?
0

小结

  • 字符判断
    • -z
    • ==
    • !=
  • 整数数值判断
    • -eq
    • -ne
    • -gt
    • -ge
    • -lt
    • -le
  • 文件与目录判断
    • -e
    • -f
    • -d
    • -r
    • -w
    • -x

相关文章

网友评论

      本文标题:06 - shell条件判断

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