chatchai_j icon

can-user-access-dir

chatchai_j | PRO | 09/04/20 09:35:16 AM UTC | 0 ⭐ | 1572 👁️ | Never ⏰ | []
Bash |

460 B

|

None

|

0 👍

/

0 👎

#!/bin/sh
 
[ "`id -u`" != 0 ] && echo "Must be root to run this script" && exit -1
[ -z "$2" -o "$3" ] && echo "Usage: $0 user directory" && exit -1
 
USER="$1"
DIR="$2"
 
id "$USER" >& /dev/null || {
    echo "User '$USER' not exist";
    exit -1;
}
 
[ ! -d "$DIR" ] && echo "Directory '$DIR' not exist" && exit -1
 
sudo -u "$USER" test -d "${DIR}/."
 
if [ "$?" != 0 ]; then
    echo "FAILED: $USER -> $DIR"
    exit 1
else
    echo "OK: $USER -> $DIR"
    exit 0
fi
 

Comments