判断当前用户是否对目录有写权限

Tyi*_*ilo 9 shell permissions

我知道您可以通过执行以下操作来确定目录的所有者:

ls -ld ~/foo | awk '{ print $3 }'
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过执行以下操作将其与当前用户进行比较:

if [[ $(ls -ld ~/foo | awk '{ print $3 }') == "$USER" ]] # or $(id -u -n ) instead of $USER
then
    echo "You are the owner"
else
    echo "You are NOT the owner"
fi
Run Code Online (Sandbox Code Playgroud)

但是您可以在不是所有者的情况下拥有写入权限。你如何确定这一点?

enz*_*tib 20

我想

if [ -w ~/foo ]; then ....
Run Code Online (Sandbox Code Playgroud)

应该做你想做的。

此外,stat -c %U ~/foo与解析ls输出相比,这是获得所有者的更好方法。