Test if a directory is writable by a given UID?

后端 未结 8 1596
一生所求
一生所求 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:14

    alias wbyu='_(){ local -i FND=0; if [[ $# -eq 2 ]]; then for each in $(groups "$1" | awk "{\$1=\"\";\$2=\"\"; print \$0}"); do (($(find "${2}" \( -perm /220 -o -group "$each" -a -perm /g+w \) 2>/dev/null | wc -l))) && FND=1; done; else echo "Usage: wbyu "; fi; (($FND)) && echo "Eureka!"; }; _'

    I put it into an alias it takes two arguments, the first is the user and the second is the directory to check. It looks for permissions writable by anyone and also loops over the groups of the specified user to check if the directory is in the user group and writable - if either gets a hit it sets a found flag and prints Eureka! at the end.

    IOW:

    FND=0
    USER=user1
    DIR=/tmp/test
    for each in $(groups "$USER" | awk '{$1="";$2=""; print $0}'); do 
    (($(find "$DIR" \( -perm /220 -o -group "$each" -a -perm /g+w \)\ 
       2>/dev/null | wc -l))) && FND=1 
    done
    (($FND)) && echo 'Eureka!'
    

提交回复
热议问题