bash 检查文件所有者权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7910582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:08:23  来源:igfitidea点击:

Checking file owner permissions

bashpermissions

提问by aport002

I'm trying to test if a file has the execute bit set for the owner in bash script.

我正在尝试测试文件是否在 bash 脚本中为所有者设置了执行位。

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?

我知道if [ -x filename ]检查运行语句的用户的执行权限,但我需要知道所有者是否拥有它。有没有办法指定所有者?

回答by Chriszuma

You can use statto get the file permissions, and parse them with another command to get the character you want.

您可以使用stat获取文件权限,并使用另一个命令解析它们以获得您想要的字符。

stat -c %A someFile

Returns something like:

返回类似:

-rw-rw-r--

EDIT:Here you go:

编辑:给你:

stat -c %A someFile | sed 's/...\(.\).\+//'

Returns either -or xif the owner has execute.

返回-或者x如果所有者已执行。

EDIT 2:For completion's sake:

编辑2:为了完成:

if [ `stat -c %A someFile | sed 's/...\(.\).\+//'` == "x" ] 
then
  echo "Owner has execute permission!"
fi

EDIT 3:If you prefer numerical permissions:

编辑 3:如果您更喜欢数字权限:

stat -c %a /path/to/a/filewill output 600 or 700 or whatever 3 digit base-8 number.

stat -c %a /path/to/a/file将输出 600 或 700 或任何 3 位基数为 8 的数字。