【发布时间】:2014-02-06 22:39:52
【问题描述】:
我正在尝试编写一个 shell 脚本,允许用户使用 chmod 命令输入权限、目录名和要排除的文件。我似乎无法让它正常工作。我对 shell 脚本很陌生,所以它可能是一个简单的语法错误。我的代码如下所示:
#!/bin/bash
clear
echo " ====================================
We need our rights!
Set file permissions!
Only use numerical representation
of permissions listed below!
====================================
1) r. read access to a file
2) w. write access to a file
3) x. Execute access to a file "
echo "Please enter a permission:"
read permish
echo
echo "Please enter your directory name:"
read directory
echo
echo "Please enter a file to exclude:"
read exclFile
perm=""
if [ "$permish" -eq 1 ]; then
"$perm" = "u+r"
elif [ "$permish" -eq 2 ]; then
"$perm" = "u+w"
elif [ "$permish" -eq 3 ]; then
"$perm" = "u+x"
else
echo "invalid input"
fi
<chmod perm= "$perm" >
<fileset dir= "$directory" >
<exclude name= "**/$exclFile" />
</fileset>
</chmod>
echo "Done!"
Stephan Ferraro 的回答对我帮助不大,但我解决了我的问题。我只是以不同的方式去做。结果是这样的:
#!/bin/bash
clear
echo " ====================================
We need our rights!
Set file permissions!
Only use numerical representation
of permissions listed below!
====================================
1) r. read access to a file
2) w. write access to a file
3) x. Execute access to a file "
echo "Please enter a permission number
Exit with [x]:"
read permish
echo "Please enter a directory name:"
read dir
echo "Please enter a file to exclude:"
read xcldfile
case "$permish" in
1)
chmod -R u+r $dir
chmod u-r */$xcldfile
;;
2)
chmod -R u+w $dir
chmod u-w */$xcldfile
;;
3)
chmod -R u+x $dir
chmod u-x */$xcldfile
;;
x)
exit;;
*)
echo "invalid input. Try again"
sleep 2
bash HW2P1.sh
;;
esac
echo "Done!"
【问题讨论】: