shell – 如何知道给定用户是否具有给定路径的读取和/或写入权限

我应该首先说我认为自己是一个熟练的用户.但是今天我需要自动化这个并且被困住了.

让我们假设我是root用户,因此我能够遍历整个文件系统,但我不能运行“sudo”或“su”.

我有一个给定的用户一个给定的路径.

如何通过CLI检查用户是否能够读取和/或写入路径?

我知道这听起来很容易,但请记住:

>我们不是,也不能成为用户.
>我们不能仅仅依赖于最终目录权限,因为路径上方的权限可能阻止访问最终目录.
>在这个继承组的时代,我们不能仅依赖目录组权限.

我假设这不能通过任何命令完成,我需要首先收集所有用户组,然后遍历路径的整个层次结构,检查整个路径上的读取权限,然后阅读&写为最终目录.听起来非常昂贵.

给我一个脚本专家!
check_access() {
  checked_file=$1
  target_user=$2
  result=PASS

  groups=`id -G $target_user | sed -e 's| | -o -group |g' -e 's|^|\\( -group |' -e 's|$| \\)|'`

  while [ $checked_file != / ]; do 
    find $checked_file -maxdepth 0 \
      -type f \( \
        \( -user $target_user -perm 0400 \) \
        -o \( $groups -perm 0040 \) \
        -o -perm 0004 \
      \) -o -type d \( \
        \( -user $target_user -perm 0100 \) \
        -o \( $groups -perm 0010 \) \
        -o -perm 0001 \
      \) >/dev/null 2>&1 || result=FAIL
    checked_file=`dirname $checked_file`
  done
  echo $result
}

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...