Test if a directory is writable by a given UID?

后端 未结 8 1593
一生所求
一生所求 2020-12-14 14:31

We can test if a directory is writable by the uid of the current process:

if [ -w $directory ] ; then echo \'Eureka!\' ; fi

But can anyone

8条回答
  •  鱼传尺愫
    2020-12-14 15:17

    Because I had to make some changes to @chepner's answer in order to get it to work, I'm posting my ad-hoc script here for easy copy & paste. It's a minor refactoring only, and I have upvoted chepner's answer. I'll delete mine if the accepted answer is updated with these fixes. I have already left comments on that answer pointing out the things I had trouble with.

    I wanted to do away with the Bashisms so that's why I'm not using arrays at all. The ((arithmetic evaluation)) is still a Bash-only feature, so I'm stuck on Bash after all.

    for f; do
        set -- $(stat -Lc "0%a %G %U" "$f")
        (("$1" & 0002)) && continue
        if (("$1" & 0020)); then
            case " "$(groups "$USER")" " in *" "$2" "*) continue ;; esac
        elif (("$1" & 0200)); then
            [ "$3" = "$USER" ] && continue
        fi
        echo "$0: Wrong permissions" "$@" "$f" >&2
    done
    

    Without the comments, this is even fairly compact.

提交回复
热议问题