2014-02-06 62 views
1

我正在嘗試編寫一個shell腳本,允許用戶輸入使用chmod命令排除的權限,目錄名和文件。我似乎無法讓它正常工作。我對shell腳本非常陌生,所以它可能是一個簡單的語法錯誤。我的代碼如下所示:在Unix Shell腳本中分配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:" 
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!" 

斯蒂芬費拉羅的回答對我並不是很有幫助,但我解決了我的問題。我只是以不同的方式去了解它。這是結果:

#!/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!" 

回答

1

你的腳本不是一個bash兼容的文件: XML標籤就像什麼都沒有在這個腳本來執行。要排除文件,必須首先通過排除此列表中的文件創建目錄的文件列表,然後將chmod應用於未排除的文件。有用的命令是ls/find,grep -v,xargs -n1。請查看手冊頁以獲取更多信息。

+0

您的解決方案並不完整:不包括對$ xcldfile中的文件根本不執行「chmod」的方法。 –