在Unix Shell脚本中分配chmod权限并排除文件
我正在尝试编写一个shell脚本,允许用户使用chmod命令输入权限、目录名和要排除的文件。我似乎无法使它正常工作。我对shell脚本非常陌生,所以这可能是一个简单的语法错误。我的代码如下所示:在Unix Shell脚本中分配chmod权限并排除文件,shell,unix,chmod,Shell,Unix,Chmod,我正在尝试编写一个shell脚本,允许用户使用chmod命令输入权限、目录名和要排除的文件。我似乎无法使它正常工作。我对shell脚本非常陌生,所以这可能是一个简单的语法错误。我的代码如下所示: #!/bin/bash clear echo " ==================================== We need our rights! Set file permissions! Only use numerical representa
#!/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!"
您的脚本不是符合bash的文件:
像这样的XML标记在此脚本中没有任何作用。要排除文件,必须首先通过排除此列表中的文件来创建目录的文件列表,然后对未排除的文件应用chmod。对此有用的命令有ls/find、grep-v、xargs-n1。有关详细信息,请查看手册页。您的解决方案不完整:排除意味着对$xcldfile中的文件根本不执行“chmod”。
#!/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!"