Bash脚本测试给定用户是否可以读取目录和所有文件?

Mic*_*ael 5 filesystems bash su

如果给定用户可以读取包含所有文件和子目录的给定目录,我需要在bash脚本中进行测试.该脚本以root身份运行.

假设给定的用户名是$user和要测试的目录,$dir我将以下行添加到脚本中

su -m $user -c "test -r $dir && test -x $dir"
if [ $? -ne ]; then
   echo "$dir is not readable or executable"
fi

你会建议改正吗?

dev*_*ull 4

你可以简单地说:

su -m $user -c "find $dir >/dev/null 2>&1 || echo $dir is not readable or executable"
Run Code Online (Sandbox Code Playgroud)

如果其中的任何文件/目录不可读,这将生成不可读或可执行消息。$dir

find $dir如果无法读取任何文件,将返回非零错误代码。


编辑:查找所有不可读的目录/文件的更完整(或可靠)的方法是:

find . \( -type d -perm /u+r -o -type d -perm /u+x -o -type f -perm /u+r \)
Run Code Online (Sandbox Code Playgroud)