检查文件所有者权限 [英] Checking file owner permissions
问题描述
我正在尝试测试文件是否在bash脚本中为所有者设置了执行位.
I'm trying to test if a file has the execute bit set for the owner in bash script.
我知道if [ -x filename ]
检查运行该语句的用户的执行许可权,但我需要知道所有者是否拥有它.有没有办法指定所有者?
I know if [ -x filename ]
checks for execute permission for the User running the statement but i need to know if the owner has it. Is there a way to specify owner?
推荐答案
您可以使用stat
获取文件权限,然后使用另一个命令解析它们以获取所需的字符.
You can use stat
to get the file permissions, and parse them with another command to get the character you want.
stat -c %A someFile
返回类似的内容
-rw-rw-r--
,开始:
stat -c %A someFile | sed 's/...\(.\).\+/\1/'
如果所有者已执行,则返回-
或x
.
Returns either -
or x
if the owner has execute.
为完成起见:
if [ `stat -c %A someFile | sed 's/...\(.\).\+/\1/'` == "x" ]
then
echo "Owner has execute permission!"
fi
:如果您希望使用数字权限:
EDIT 3: If you prefer numerical permissions:
stat -c %a /path/to/a/file
将输出600或700或任何3位以8为底的数字.
stat -c %a /path/to/a/file
will output 600 or 700 or whatever 3 digit base-8 number.
这篇关于检查文件所有者权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!