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

简介:

脚本名:power.sh

脚本内容:

Shell代码   收藏代码
  1. #!/bin/sh  
  2. username3=$1  
  3. dir_name2=$2  
  4.   
  5. # get existing directory  
  6. file_path=$dir_name2  
  7. while true  
  8. do  
  9.         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  
  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 具有读和写权限,没有执行权限)。
相关文章
|
2月前
|
Linux 数据安全/隐私保护 Windows
Linux共享Windows目录
Linux共享Windows目录
|
2天前
|
运维 监控 安全
Linux_权限理解(详细PLUS)
【10月更文挑战第3天】本文介绍了Linux系统中的权限管理基础,包括文件系统对象与权限关联、权限的继承性、字符与数字表示法的解读,以及用户、组与权限的动态交互。详细解析了`chmod`命令的高级用法和权限修改的风险,探讨了SUID、SGID和Sticky Bit等特殊权限的作用机制,并提出了基于角色的权限分配和定期权限审计的最佳实践。
21 11
|
8天前
|
Linux
深入理解Linux中的cp命令:文件与目录的复制利器
深入理解Linux中的cp命令:文件与目录的复制利器
|
1月前
|
Linux 应用服务中间件 nginx
|
8天前
|
Linux Shell Python
9-7|salt代码在linux机子那个目录
9-7|salt代码在linux机子那个目录
|
8天前
|
数据可视化 Ubuntu Linux
8-14|如何查看linux目录下文件大小
8-14|如何查看linux目录下文件大小
|
2月前
|
Linux
linux如何去掉目录背景颜色
linux如何去掉目录背景颜色
44 1
|
2月前
|
JSON Linux 网络安全
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
|
2月前
|
Unix Linux Go
Linux中的chmod权限问题
Linux中的chmod权限问题
30 0
|
2月前
|
Shell Linux 开发工具
在Linux中,如何编写shell脚本将当前目录下大于10K的文件转移到/tmp目录下?
在Linux中,如何编写shell脚本将当前目录下大于10K的文件转移到/tmp目录下?
下一篇
无影云桌面