Bash测试一个目录是否由给定的UID可写?

我们可以测试目录是否可以通过当前进程的uid写入:
$ if [ -w $directory ] ; then echo 'Eureka!' ; fi

但任何人可以建议一种方法来测试一个目录是否可写的一些其他uid?

我的方案是,我管理一个MysqL服务器实例,我想要临时更改慢查询日志文件的位置。我可以通过执行一个MySQL命令SET GLOBAL slow_query_log_file =’$ new_log_filename’,然后禁用&启用查询日志记录以使MysqLd开始使用该文件

但是我想让我的脚本检查MysqLd进程的uid有权创建新的日志文件。所以我想做一些像(伪码):

$ if [ -w-as-MysqL-uid `basename $new_log_filename` ] ; then echo 'Eureka!' ; fi

但是,这当然是一个想象的测试谓词。

澄清:我想要一个不依赖su的解决方案,因为我不能假设我的脚本的用户有su权限。

这里有一个长,迂回的检查方式。
USER=johndoe
DIR=/path/to/somewhere

# Use -L to get information about the target of a symlink,# not the link itself,as pointed out in the comments
INFO=( $(stat -L -c "%a %G %U" $DIR) )
PERM=${INFO[0]}
GROUP=${INFO[1]}
OWNER=${INFO[2]}

ACCESS=no
if [[ $PERM & 0002 != 0 ]]; then
    # Everyone has write access
    ACCESS=yes
elif [[ $PERM & 0020 != 0 ]]; then
    # Some group has write access.
    # Is user in that group?
    gs=( $(groups $USER) )
    for g in "${gs[@]}"; do
        if [[ $GROUP == $g ]]; then
            ACCESS=yes
            break
        fi
    done
elif [[ $PERM & 0200 != 0 ]]; then 
    # The owner has write access.
    # Does the user own the file?
    [[ $USER == $OWNER ]] && ACCESS=yes
fi

相关文章

用的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补全...