脚本名:power.sh
脚本内容
- #!/bin/sh
- username3=$1
- file_path=$2
- if [ `id -u` -ne 0 ]; then
- echo “Please re-run `basename $0` as root.”
- exit 1
- fi
- # Get existing directory
- 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
- 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_int=0
- is_exe_int=0
- is_write_int=0
- if [ $is_read -eq 0 ];then
- is_read_int=100
- fi
- if [ $is_exe -eq 0 ];then
- is_exe_int=1
- fi
- if [ $is_write -eq 0 ];then
- is_write_int=10
- fi
- $result_and
- type let > /dev/null 2>&1
- if [ $? -eq 0 ];then
- let result_and=is_read_int+is_exe_int
- let result_and=result_and+is_write_int
- else
- result_and=`expr $is_read_int + $is_exe_int`
- result_and=`expr $result_and + $is_write_int`
- fi
- exit $result_and
应用:
sh power.sh user1 /opt/abc
若返回值为111,则表示用户user1 对目录 /opt/abc具有读写执行权限;
若返回值为101,则表示用户user1 对目录 /opt/abc具有读执行权限;
若返回值为10,则表示用户user1 对目录 /opt/abc具有写权限;
注意:必须以 root 执行 power.sh