linux如何判断指定用户对指定目录具有的权限具体介绍

王朝学院·作者佚名  2016-08-08
窄屏简体版  字體: 小  |  中  |  大  |  超大  

脚本名:power.sh

脚本内容:

代码如下:

#!/bin/sh

username3=$1

dir_name2=$2

# get existing directory

file_path=$dir_name2

while true

do

if [ -d $file_path ];then

break;

fi

file_path=${file_path%/*}

done

dir_name2=$file_path

# Judge whether the user exists

grep "^$username3:" /etc/passwd >/dev/null

if [ $? -ne 0 ];then

echo "This user "$username3" does not exist."

exit 4

fi

#echo "username : $username3"

group4=` grep "^$username3:" /etc/passwd |awk -F : {'PRint $4'}|xargs -i grep {} /etc/group|cut -d":" -f1`

#echo "group : $group4"

su -l $username3 -c "test -r $dir_name2"

is_read=$?

su -l $username3 -c "test -x $dir_name2"

is_exe=$?

su -l $username3 -c "test -w $dir_name2"

is_write=$?

$is_read_str

$is_exe_str

$is_write_str

if [ $is_read -eq 0 ];then

is_read_str="r"

else

is_read_str="-"

fi

if [ $is_exe -eq 0 ];then

is_exe_str="x"

else

is_exe_str="-"

fi

if [ $is_write -eq 0 ];then

is_write_str="w"

else

is_write_str="-"

fi

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 具有读和写权限,没有执行权限)。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航