【问题标题】:Assigning chmod permissions and excluding a file in a Unix Shell Script分配 chmod 权限并排除 Unix Shell 脚本中的文件
【发布时间】: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!"

【问题讨论】:

    标签: shell unix chmod


    【解决方案1】:

    您的脚本不是 bash 兼容文件: XML 标记之类的与此脚本无关。要排除文件,您必须首先通过排除此列表中的文件来创建目录的文件列表,然后对未排除的文件应用 chmod。对此有用的命令是 ls/find、grep -v、xargs -n1。请查看手册页以获取更多信息。

    【讨论】:

    • 您的解决方案不完整:排除意味着对$xcldfile中的文件根本不执行“chmod”。
    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2011-06-25
    • 2016-01-20
    相关资源
    最近更新 更多