判断条件

判断条件:
-d 判断该文件是否存在,并且是目录(是目录为真)
-e 判断该文件是否存在(存在为真)
-f 判断该文件是否存在,并且是否普通文件(是普通文件为真)
-w 是否有写权限
-r 是否有读权限
-x 是否有执行权限

两种格式
test -e 文件
[-e 文件]

[root@localhost tmp]# test -e student.txt
[root@localhost tmp]# echo $?
0 //输出0,上一条命令正确执行
[root@localhost tmp]# test -e 232
[root@localhost tmp]# echo $?
1 //输出1,上一条命令错误执行

[root@localhost tmp]# [ -e student.txt ]
[root@localhost tmp]# echo $?
0

另一种方式
[root@localhost tmp]# [ -e student.txt ] && echo "yes" || echo "no"
yes


文件1 -nt 文件2 判断文件1比文件2新
文件1 -ot 文件2 判断文件1比文件2旧
文件1 -ef 文件2 判断文件1是否和文件2的Inode号一致,可以理解为两个文件是否是同一个文件。可以判断硬链接。

创建硬链接:
ln /root/student.txt /tmp/stu.txt
[ /root/student.txt -ef /tmp/stu.txt ] && echo yes || echo no
yes

两个整数之间的比较:
整数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

-z 字符串 为空则返回真
-n 字符串 判断字符串是否为非空
字符串1==字符串2 判断字符串1是否和字符串2相等
字符串1!=字符串2 判断字符串1是否和字符串2不相等

多重条件判断:
判断1 -a 判断2 都成立才成立
判断1 -o 判断2 有一个成立就成立
!判断 取反

[root@localhost tmp]# aa=23
[root@localhost tmp]# [ -n "$aa" -a "$aa" -gt 22 ] && echo yes || echo no
yes

原文地址:https://www.cnblogs.com/javasl/p/11155156.html