使用 awk 在 UNIX 中处理文件权限

Handling file permissions in UNIX using awk

我想知道使用 shell 脚本授予文件的权限。所以我使用下面的代码来测试文件。但它在输出中没有显示任何内容。我只是想知道我在哪里犯了错误。请帮我。 文件“1.py”已启用所有读写和执行文件。

 ls -l 1.py | awk ' {if( -eq "-rwxrwxrwx")print 'True'; }'

True两边的单引号(')应该是双引号("),awk使用==进行字符串比较。

但是,根据您要执行的操作,使用 Bash 内置测试可能更简洁:

if [ -r 1.py -a -x 1.py ]; then
  echo "Yes, we can read (-r) and (-a) execute (-x) the file"
else
  echo "No, we can't."
fi

这避免了必须解析 ls 输出。有关更长的检查列表,请参阅 tldp.org

在 awk 中,您不应该编写 shell 测试,例如[[ ... -eq ...]],你应该用 awk 的方式来做:

if(=="whatever")...

你可以使用

ls -l 1.py | awk '{if ( ==  "-rwxrwxrwx") print "True" }'