Linux判断指定用户对指定目录具有的权限


Linux #目录权限 #命令2012-11-15 14:12
脚本名:power.sh

脚本内容:

01#!/bin/sh
02username3=$1
03dir_name2=$2
04 
05# get existing directory
06file_path=$dir_name2
07while true
08do
09        if [ -d $file_path ];then
10                break;
11        fi
12        file_path=${file_path%/*}
13done
14dir_name2=$file_path
15 
16# Judge whether the user exists http://yige.org
17grep "^$username3:" /etc/passwd >/dev/null
18if [ $? -ne 0 ];then
19    echo "This user \"$username3\" does not exist."
20    exit 4
21fi
22#echo "username : $username3"
23group4=` grep "^$username3:"  /etc/passwd |awk -F : {'print $4'}|xargs  -i  grep {}  /etc/group|cut -d":" -f1`
24#echo "group : $group4"
25su -l $username3 -c "test -r $dir_name2"
26is_read=$?
27su -l $username3 -c "test -x $dir_name2"
28is_exe=$?
29su -l $username3 -c "test -w $dir_name2"
30is_write=$?
31$is_read_str
32$is_exe_str
33$is_write_str
34if [ $is_read -eq 0 ];then
35    is_read_str="r"
36else
37    is_read_str="-"
38fi
39 
40if [ $is_exe -eq 0 ];then
41        is_exe_str="x"
42else
43        is_exe_str="-"
44fi
45 
46if [ $is_write -eq 0 ];then
47        is_write_str="w"
48else
49        is_write_str="-"
50fi
51 
52 
53echo "${is_read_str}${is_write_str}${is_exe_str}"


注意:必须以root 身份执行该脚本。
脚本power.sh 需要两个参数,第一个表示指定的用户,第二个表示指定的目录

测试:
[root@ppc40 study]# sh power.sh whuanga4 /tmp/abc/dd
This user "whuanga4" does not exist.
[root@ppc40 study]# sh power.sh whuang4 /tmp/abc/dd
rw-

(说明:表示用户whuang4 对目录/tmp/abc/dd 具有读和写权限,没有执行权限)。


相关文章

粤ICP备11097351号-1