判断给出的条件真假,如果为真,它的执行状态为0,否则为非0。
- -e判断文件是否存在
- -f 普通文件
- -L 链接文件
- -d 目录
- -x 执行权限
- -s 判断文件是否是空文件
1、判断文件是否存在
cd bin
if test -e f.txt; then echo '文件已存在!'; else echo '文件不存在!'; fi
shell提供了与(-a)、或(-o)、非(!)逻辑操作符用于连接测试条件,优先级为:非>与>或
2、判断文件是否为目录
cd bin
if test -d aa.txt; then echo '目录!'; else echo '非目录文件!'; fi
3、判断文件是否为空
cd bin
if test -s /root/aa.txt; then echo '非空文件!'; else echo '空文件'; fi
4、同时判断多个文件
cd bin
if test -e ./notFile -o -e ./bash; then echo '至少有一个文件存在!'; else echo '两个文件都不存在'; fi
5、判断文件是否为普通文件
cd bin
if test -f /root/aa.txt; then echo '普通文件!'; else echo '非普通文件'; fi
6、判断文件是否为链接文件
cd bin
if test -L /root/aa.txt; then echo '链接文件!'; else echo '非链接文件'; fi
7、判断文件是否为目录文件
cd bin
if test -d /root/aa.txt; then echo '目录文件!'; else echo '非目录文件'; fi
8、判断文件是否有执行权限
cd bin
if test -x /root/aa.txt; then echo '有执行权限!'; else echo '无执行权限'; fi