我有作业(Shell 脚本),我尽了最大努力使它工作,但徒劳无功。
有人能帮忙吗?
问题:
编写一个名为 fix-permissions.sh 的 bash 程序,它接受用户列表作为参数。
如果将一个或多个用户作为参数,脚本应按如下方式重置文件权限:
一个。目录 ~/share 到 750(如果存在)。
b. ~/share 中的所有常规文件到 744.
C。 ~/share 里面的所有目录到 750.
d. ~ 到 600 中的所有其他常规文件和 ~ 到 700 中的所有其他目录。
e.其他类型的文件保持原样。我们假设~(~/share除外)里面只有一层文件和目录,~/share里面只有一层文件和目录。
如果未提供任何参数,脚本应识别系统中的普通用户 (UID >= 500),并为每个用户重置文件权限,如上所述。
在执行结束时,此脚本必须生成一个名为 report.txt 的文件,其中包含用户主目录中所有文件的列表,包括每个文件的完整路径、所有者、组和权限。
我现在的样子是这样的:
[...没有任何内容...回到修订历史 - 这是来自修订版 4...]
第 1 部分
#!/bin/bash
#$ fx-permiss.sh user1 user2 user3
for user ; do
if -d /home/$user/share ; then
chmod 750 /home/$user/share ; else
echo “No folder named “share” is in $user home directory”
fi
find /home/$user/share -type f -exec chmod 744 {} \;
find /home/$user/share -type d -exec chmod 750 {} \;
find ~ -type f -not \( -name share -o -name ~ \) -exec chmod 600
find ~ -type d -not \( -name share -o -name ~ \) -exec chmod 700
done
第 2 部分
如果没有给出任何参数,脚本应该识别系统中的普通用户(UID >= 500),并为每个用户重置文件权限,如上所述。
for User in $(cat /etc/passwd) ; do
UserId=$(echo $User | cut -d ":" -f 3)
if ($UserId -gt 500) ; then
# reset permission
fi
done
第 3 部分
在执行结束时,生成一个名为 report.txt 的文件,其中包含用户主目录中所有文件的列表,包括每个文件的完整路径、所有者、组和权限。
find . -ls | awk '{print $user1 "\t" $5 "\t" $6 "\t" $user}' | tee report.txt
#it should be for all users
#where it should be saved?
chmod 755 fix-permissions
exit 0
最佳答案
初始帖子中的代码似乎已经消失了...汇总了一个示例。
#!/bin/bash
function mod {
if [ -d "/Users/$1/share" ]; then
echo "permissions changes for $1"
#chmod 750 /Users/$1/share
#find /Users/$1/share -type f -exec chmod 744 {} \;
#find /Users/$1/share -type d -exec chmod 750 {} \;
#find /Users/$1/ -type f -exec chmod 600 {} \;
#find /Users/$1/ -type d -exec chmod 700 {} \;
fi
}
function clean {
IFS=':' read -ra pw <<< "$1"
local c=0
local n=""
local t=500
for i in "${pw[@]}"; do
if [ $c == 0 ]; then
n="$i"
fi
if [ $c == 2 ]; then
if [ "$i" -gt "$t" ]; then
mod $n
fi
fi
c=$[$c+1]
done
}
function report {
export user=$(whoami)
ls -la "/Users/$user" > report.txt
}
if [ -z $1 ]; then"
while read line; do
if [ "${line:0:1}" != "#" ]; then
clean $line
fi
done < /etc/passwd
else
for arg in "$@"
do
mod $arg
done
fi
report
唯一不满足的要求是在报告的#3 下打印完整路径(仅打印相对路径)。变量 t 是权限更改将影响的最低 uid。注释掉了 chmod,这样就不会有人不小心对他们的系统这样做了。糟糕。
关于Bash 脚本,需要帮助⠀作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4045700/