我需要创建一个脚本,通过询问用户是否愿意为三个权限区域中的每一个区域一一启用读取、写入和/或执行,从而允许指定文件以交互方式更改其权限。确定新权限后,需要将它们应用到文件。
我花了一个多小时研究如何开始为此编写脚本,但没有成功。这对我来说似乎有点太复杂了,但我想学习如何做到这一点,并且非常感谢任何能够指导我朝着这个脚本的正确方向发展的建议。
答案1
如果你被发现使用这个...
#!/bin/bash
# Did user supply an argument (path to folder?)
if [ "${1}" == "" ] ; then
echo "Directory path required as argument" && exit 1
fi
# Was the arg a valid directory?
if [ ! -d "${1}" ] ; then
echo "Directory argument was invalid" && exit 1
fi
# Re assign variable
dir="${1}"
# Get a list of files in directory
files=$(ls ${dir})
# Loop over files and ask questions
while file in "${files}" ; do
# Prompt user for permissions to be set on user/group/owner
read -p "Set permission for (u)ser/(g)roup/(o)wner? [u|g|o]" who
# Prompt user for read/write/execute permission to be set
read -p "Add read/write/execute to ${file}? [r|w|x]" ans
# Set the specified permission for the specified account type
chmod ${who}=${ans} ${file}
done
它确实应该提供一个循环来应用每个文件的多个帐户类型读/写/执行位,但已经晚了。