测试/etc/profile和/root/t1.sh是否有执行权限
代码:
方法1:
#!/bin/bash
if [ -x /etc/profile ]; then
echo " Yes x mode - /etc/profile"
else
echo " No x mode - /etc/profile"
fi
if [ -x ./t1.sh ]; then
echo " Yes x mode - t1.sh "
else
echo " No x mode - t1.sh!!!"
fi
方法2:
#!/bin/bash
x1=$(ls -l /etc/profile | awk '{print $1}'|grep "x"|wc -l)
if [ $x1 -gt 0 ]; then
echo "Yes x modx - /etc/profile"
else
echo "No x modx - /etc/profile"
fi
x2=$(ls -l /root/t1.sh | awk '{print $1}'|grep "x"|wc -l)
if [ $x2 -gt 0 ]; then
echo " Yes x modx - /root/t1.sh"
else
echo "No x modx - /root/t1.sh"
fi
总结:
方法1利用shell的“-x”逻辑判断方法确定文件存在且执行
方法1利用ls -l命令输出关于权限的内容进行判断。虽然未进行判断文件是否存在,但输出的错误提示被awk截取第一字段内容不包含x,所以判断结果也是正确的。