Linux判断指定用户对指定目录具有的权限
Linux #目录权限 #命令2012-11-15 14:12
脚本名:power.sh
脚本内容:
01 | #!/bin/sh |
02 | username3=$1 |
03 | dir_name2=$2 |
04 |
05 | # get existing directory |
06 | file_path=$dir_name2 |
07 | while true |
08 | do |
09 | if [ -d $file_path ]; then |
10 | break ; |
11 | fi |
12 | file_path=${file_path%/*} |
13 | done |
14 | dir_name2=$file_path |
15 |
16 | # Judge whether the user exists http://yige.org |
17 | grep "^$username3:" /etc/ passwd >/dev/null |
18 | if [ $? - ne 0 ]; then |
19 | echo "This user \"$username3\" does not exist." |
20 | exit 4 |
21 | fi |
22 | #echo "username : $username3" |
23 | group4=` grep "^$username3:" /etc/ passwd | awk -F : { 'print $4' }| xargs -i grep {} /etc/group| cut -d ":" -f1` |
24 | #echo "group : $group4" |
25 | su -l $username3 -c "test -r $dir_name2" |
26 | is_read=$? |
27 | su -l $username3 -c "test -x $dir_name2" |
28 | is_exe=$? |
29 | su -l $username3 -c "test -w $dir_name2" |
30 | is_write=$? |
31 | $is_read_str |
32 | $is_exe_str |
33 | $is_write_str |
34 | if [ $is_read - eq 0 ]; then |
35 | is_read_str= "r" |
36 | else |
37 | is_read_str= "-" |
38 | fi |
39 |
40 | if [ $is_exe - eq 0 ]; then |
41 | is_exe_str= "x" |
42 | else |
43 | is_exe_str= "-" |
44 | fi |
45 |
46 | if [ $is_write - eq 0 ]; then |
47 | is_write_str= "w" |
48 | else |
49 | is_write_str= "-" |
50 | fi |
51 |
52 |
53 | echo "${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 具有读和写权限,没有执行权限)。
相关文章
- Ubuntu下查看CPU温度的方法 2012/11/14
- Linux黑客学习笔记 2012/11/09
- Linux下查找大文件或目录的方法 2012/11/08
- Linux下patch的制作和应用 2012/11/02
- Linux的PATH环境变量解析 2012/11/02
- Linux查看硬件配置 2012/11/02
- CentOS支持NTFS分区的方法 2012/11/01
- Linux中的Printk与dmesg功能详解 2012/11/01
- Linux必学命令60个 2012/11/01
- Linux系统新手学习建议 2012/11/01