我正在尝试编写一个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!"
答案 0 :(得分:1)
您的脚本不是bash兼容文件: XML标签就像在这个脚本中无关。要排除文件,您必须首先通过排除此列表中的文件来创建目录的文件列表,然后在非排除文件上应用chmod。对此有用的命令是ls / find,grep -v,xargs -n1。有关它的更多信息,请查看手册页。