Linux 中的条件判断

本部分主要介绍:Linux 的条件判断:按文件类型;按文件权限;两文件之间比较;两整数之间比较;字符串判断;多重条件判断

按照文件类型判断

  1. 按照文件类型进行判断

    • 如表格(加粗为常用选项)

      测试选项 作用
      -b file 判断该文件是否存在,并且判断是否为 块设备文件(是块设备文件为真)
      -c file 判断该文件是否存在,并且判断是否为 字符设备文件(是字符设备文件为真)
      -d file 判断该文件是否存在,并且判断是否为 目录文件(是目录文件为真)
      -e file 判断该文件是否 存在(存在为真)
      -f file 判断该文件是否存在,并且判断是否为 普通文件(是普通文件为真)
      -L file 判断该文件是否存在,并且判断是否为 符号链接文件(是符号链接文件为真)
      -p file 判断该文件是否存在,并且判断是否为 管道文件(是管道文件为真)
      -s file 判断该文件是否存在,并且判断是否为 非空(非空为真)
      -S file 判断该文件是否存在,并且判断是否为 套接字文件(是套接字文件为真)
  2. 两种判断格式

    • 命令 test -e /root 判断文件是否存在(echo $? 显示上一条命令是否正确执行)

      root@localcomputer:~# test -e test/
      root@localcomputer:~# echo $?      # 0 表示正确
      0
      root@localcomputer:~# test -e t
      root@localcomputer:~# echo $?      # 1 表示错误
      1
      
    • 命令 [ -e /root ] 判断文件是否存在(注意中括号与内容之间有空格),习惯使用此种方法

      root@localcomputer:~# [ -e ./test/ ]
      root@localcomputer:~# echo $?
      0
      root@localcomputer:~# [ -e ./tes ]
      root@localcomputer:~# echo $?
      1
      root@localcomputer:~# 
      
  3. 方便书写(不适用 echo $?

    • 命令 [ -d /root ] && echo "yes" || echo "no" ,正确直接打印 yes,反之打印 no

      root@localcomputer:~# [ -d /root ] && echo "yes" || echo "no"
      yes
      root@localcomputer:~# [ -f /root ] && echo "yes" || echo "no"
      no                 # 文件存在但不是普通文件(目录)打印 no
      root@localcomputer:~# 
      

按照文件权限判断

  1. 按照文件权限判断

    • 如表格(加粗为常用)

      测试选项 作用
      -r file 判断该文件是否存在,并且判断是否为拥有读权限(有读权限为真)
      -w file 判断该文件是否存在,并且判断是否为拥有写权限(有写权限为真)
      -x file 判断该文件是否存在,并且判断是否为拥有执行权限(有执行权限为真)
      -u file 判断该文件是否存在,并且判断是否为拥有 SUID 权限(有 SUID 权限为真)
      -g file 判断该文件是否存在,并且判断是否为拥有 SGID 权限(有 SGID 权限为真)
      -k file 判断该文件是否存在,并且判断是否为拥有 SBit 权限(有 SBit 权限为真)
    • 此命令对文件的权限判断不会区分是哪个用户拥有某种权限,而是只要有某种权限就返回真

  2. 实例

    • 命令 [ -r ./mbox ] && echo "yes" || echo "no"

      ss@localcomputer:~$ ls -ld mbox 
      -rw------- 1 ss ss 976 12月 28 22:33 mbox
      ss@localcomputer:~$ [ -r ./mbox ] && echo "yes" || echo "no"
      yes
      

两文件之间进行比较

  1. 两文件之间进行比较

    • 如表格

      测试选项 作用
      file1 -nt file2 (new than) 判断文件 1 的修改时间是否比文件 2 的新(如果新为真)
      file1 -ot file2 判断文件 1 的修改时间是否比文件 2 的旧(如果旧为真)
      file1 -ef file2 判断文件 1 是否和 文件 2 的 Inode 号一致。可以理解为两个文件是否为同一个文件,这个判断用于判断硬链接是很好的办法。
  2. 实例

    • 硬链接只能通过 Inode 号来识别(软链接有标志)使用 -ef 可以解决此问题

      1. 命令 [ grade -ef test ] && echo "yes" || echo "no"

        ss@localcomputer:~/test$ ln grade test      # 创建软链接
        ss@localcomputer:~/test$ ls -i              # 显示 Inode 节点
        357 grade  357 test
        ss@localcomputer:~/test$ ls -l              # 显示权限
        总用量 8
        -rw-r--r-- 2 ss ss 55 2月  19 20:22 grade
        -rw-r--r-- 2 ss ss 55 2月  19 20:22 test
        ss@localcomputer:~/test$ [ grade -ef test ] && echo "yes" || echo "no"  # 测试 Inode 节点
        yes
        

两个整数之间比较

  1. 两个整数之间比较

    • 如图

      测试选项 作用
      整数1 -eq 整数2 判断整数 1 是否和整数 2 相等(相等为真)
      整数1 -ne 整数2 判断整数 1 是否和整数 2 不相等(不相等为真)
      整数1 -gt 整数2 判断整数 1 是否大于整数 2 (大于为真)
      整数1 -lt 整数2 判断整数 1 是否小于整数 2 (小于为真)
      整数1 -ge 整数2 判断整数 1 是否大于等于整数 2 (大于等于为真)
      整数1 -le 整数2 判断整数 1 是否小于等于整数 2 (小于等于为真)
  2. 实例

    • 判断大小

      1. 命令 [ 99 -gt 9 ] && echo "yes" || echo "no" ,比较两数大小

        ss@localcomputer:~/test$ [ 99 -gt 9 ] && echo "yes" || echo "no"
        yes
        ss@localcomputer:~/test$ [ 99 -gt 100 ] && echo "yes" || echo "no"
        no
        ss@localcomputer:~/test$ 
        

字符串的判断(常用)

  1. 字符串的判断

    • 如表格

      测试字符 作用
      -z 字符串 判断字符串是否为空(空为真)
      -n 字符串 判断字符串是否为非空(非空为真)
      字符串1 == 字符串2 判断字符串 1 与 字符串 2 是否相等
      字符串1 !== 字符串2 判断字符串 1 与 字符串 2 是否不等相等
  2. 实例

    • 测试 -z-n

      1. 删除变量 name, 测试 [ -z "$name" ] && echo "yes" || echo "no" ,测试 变量 name 为空,和非空(注意变量加双引号)

        ss@localcomputer:~/test$ unset name
        ss@localcomputer:~/test$ [ -z "$name" ] && echo "yes" || echo "no" 
        yes                     # 空返回 yes
        ss@localcomputer:~/test$ [ -n "$name" ] && echo "yes" || echo "no"
        no                      # 空返回 no
        ss@localcomputer:~/test$ 
        

多重条件判断

  1. 多重条件判断

    • 如表格

      测试选项 作用
      判断1 -a 判断2 逻辑与,两个判断都成立,结果为真
      判断1 -o 判断2 逻辑或,有一个判断成立(两个都成立也可以),结果为真
      ! 判断 (空格) 逻辑非,与原始判断结果相反
  2. 实例

    • 测试(变量 name 为空)

      1. 命令 [ 22 -gt 10 -a -n "$name" ] && echo "yes" || echo "no"

      2. 命令 [ 22 -gt 10 -o -n "$name" ] && echo "yes" || echo "no"

      3. 命令 [ ! -n "$name" ] && echo "yes" || echo "no" ,注意 ! 判断 空格

        ss@localcomputer:~/test$ [ 22 -gt 10 -a -n "$name" ] && echo "yes" || echo "no"
        no
        ss@localcomputer:~/test$ [ 22 -gt 10 -o -n "$name" ] && echo "yes" || echo "no"
        yes
        ss@localcomputer:~/test$ [ ! -n "$name" ] && echo "yes" || echo "no"
        yes
        ss@localcomputer:~/test$ 
        
        

你可能感兴趣的:(Linux 中的条件判断)