Welcome 微信登录

首页 / 操作系统 / Linux / Linux 中判断指定用户对指定目录的访问权限

脚本名:power.sh
 
脚本内容
  1. #!/bin/sh
  2. username3=$1
  3. file_path=$2
  4. if [ `id -u` -ne 0 ]; then
  5. echo "Please re-run `basename $0` as root."
  6. exit 1
  7. fi
  8. # Get existing directory
  9. while true
  10. do
  11. if [ -d $file_path ];then
  12. break;
  13. fi
  14. file_path=${file_path%/*}
  15. done
  16. dir_name2=$file_path
  17. # Judge whether the user exists
  18. grep "^$username3:" /etc/passwd >/dev/null
  19. if [ $? -ne 0 ];then
  20. echo "This user "$username3" does not exist."
  21. exit 4
  22. fi
  23. su -l $username3 -c "test -r $dir_name2"
  24. is_read=$?
  25. su -l $username3 -c "test -x $dir_name2"
  26. is_exe=$?
  27. su -l $username3 -c "test -w $dir_name2"
  28. is_write=$?
  29. is_read_int=0
  30. is_exe_int=0
  31. is_write_int=0
  32. if [ $is_read -eq 0 ];then
  33. is_read_int=100
  34. fi
  35. if [ $is_exe -eq 0 ];then
  36. is_exe_int=1
  37. fi
  38. if [ $is_write -eq 0 ];then
  39. is_write_int=10
  40. fi
  41. $result_and
  42. type let > /dev/null 2>&1
  43. if [ $? -eq 0 ];then
  44. let result_and=is_read_int+is_exe_int
  45. let result_and=result_and+is_write_int
  46. else
  47. result_and=`expr $is_read_int + $is_exe_int`
  48. result_and=`expr $result_and + $is_write_int`
  49. fi
  50. exit $result_and
应用:
 
sh power.sh user1 /opt/abc
 
若返回值为111,则表示用户user1 对目录  /opt/abc具有读写执行权限;
 
若返回值为101,则表示用户user1 对目录  /opt/abc具有读执行权限;
 
若返回值为10,则表示用户user1 对目录  /opt/abc具有写权限;
 
注意:必须以 root 执行 power.shLinux中如何获取已存在目录Linux 获取指定目录的父目录相关资讯      Linux系统教程 
  • Linux系统教程:修复“ImportError  (08/17/2015 07:50:43)
  • 正在经历变革的 Linux 系统管理员  (08/14/2014 09:49:42)
  • Linux系统教程:定制5M的RedHat 5.  (05/18/2013 14:49:49)
  • Linux系统教程:Ubuntu桌面上禁用  (06/07/2015 14:01:48)
  • 新电脑+旧系统:教你在不同机器间  (07/04/2013 06:51:32)
  • Linux系统教程:自动删除n天前日志  (03/25/2013 19:10:16)
本文评论 查看全部评论 (0)
表情: 姓名: 字数