第84集 运算符(4) 3.5、文件测试运算符(重点) 文件测试运算符用于检测Unix/Linux文档的属性。 属性检查描述如下 运算符 说明 举例 -b file 检测文件是否为块设备文件 [ -b $file ]返回false -c file 检测文件是否为字符设备文件 [ -c $file ]返回false -d file※ 检测文件是否为目录 [ -d $file ]返回false -f file※ 检测文件是否为普通文件(既不是目录,也不是设备文件) [ -f $file ]返回true -g file 检测文件是否设置了SGID位 [ -g $file ]返回false -k file 检测文件是否设置了粘着位(Sticky Bit) [ -k $file ]返回false -p file 检测文件是否是有名管道 [ -p $file ]返回false -u file 检测文件是否设置了SUID位 [ -u $file ]返回false -r file※ 检测文件是否可读 [ -r $file ]返回false -w file※ 检测文件是否可写 [ -w $file ]返回true -x file※ 检测文件是否可执行 [ -x $file ]返回true -s file※ 检测文件是否不为空 [ -s $file ]返回true -e file※ 检测文件(目录)是否存在 [ -e $file ]返回true 【案例:】 filepath=/home/cspexpert/test.sh if [ −d $filepath ];then echo "是目录";else echo "不是目录";fi if [ −f $filepath ];then echo "是普通文件";else echo "不是普通文件";fi if [ −r $filepath ];then echo "可读";else echo "不可读";fi if [ −w $filepath ];then echo "可写";else echo "不可写";fi if [ −x $filepath ];then echo "可执行";else echo "不可执行";fi if [ −s $filepath ];then echo "不为空";else echo "为空";fi if [ −e $filepath ];then echo "存在";else echo "不存在";fi 【执行结果:】 根据实际输出 【注意:】 ①上表中的“文件”理解范围可以包括“目录”。 ②判断结果是根据执行当前shell脚本的用户权限决定,而不一定是文档的真实情况。 ---------------------------------------------------------------------------------------------------------------------- 我们尊重原创,也注重分享,文章来源于微信公众号:蒲公英的头发,建议关注公众号查看原文。如若侵权请联系qter@qter.org。 ---------------------------------------------------------------------------------------------------------------------- |