2010-12-06 91 views
0

我需要一些帮助才能运行。只有两个部分。需要修复的两个bash函数

我是bash脚本编程的新手。我怎么重复这个,直到输入一个文件名? PS它似乎没有工作一段时间,直到我试过了。

ifSpaces(){ 
#if more than one file name if statement(checks for spaces) 
if [ "$#" -eq "$(echo "[email protected]" | wc -w)" ] 
then 
echo -n "Please enter a filename: "; 
read filename 
else 
echo -n "Please enter a single filename! "; 
fi 
} 

这个函数应该测试文件是否可以写入。它似乎通过它,而不是测试它的权利。但我不确定。基本上,它有什么问题,请纠正,因为我没有得到它时,人们告诉我,而不是告诉我如何。

#how do I get this to work? 
testFiles(){ 
#loop through files and test each one 
for filename in "[email protected]" 
do 
filename="[email protected]" 
# put this in a loop that grabs all the values. 
# test all the file names 
while [ -f "$filename" ] 
do 
if [ -w $filename ] 
then 
echo "The file exists and is writable"; 
overWriteFile 
saveResults 

elif [ -d $filename ] 
then 
read filename 
echo "$filename"; 
echo "The file you specified exists and is a directory". 
saveResults 

else 
>$directory$filename; 
fi 
done 
echo "$filename"; 
echo "The file you specified exists and is neither a regular file nor a directory."; 

done 
saveResults 

} 
+1

请在您的代码上使用缩进 - 它可以帮助人们对所写内容做出正面或反面的评论。 – 2010-12-06 01:51:16

回答

3

ifSpaces()函数需要更好的定义 - 它是做什么的?

  • 取一个参数,一个可能的文件名。
  • 如果该参数不包含空格,则返回它。
  • 如果该参数包含空格,则会提示输入新的文件名,直到给出不含空格的名称。

如何测试字符串(又名文件名)中的空格?

spacesInName() 
{ 
    case "$1" in 
    (* *) return 0;; 
    (*) return 1;; 
    esac 
} 

因此:

ifSpaces() 
{ 
    filename=$1 
    while spacesInName "$filename" 
    do 
     echo -n "Enter a filename without spaces: " 
     read filename 
    done 
    return $filename 
} 

确定 - 即做,现在我们要问 “为什么?”。为了确保你的代码能够与任何有效的文件名一起工作,不是更好吗?因为所有的主操作系统(文件系统)都能识别其中有空格的文件名,并将其视为有效的?

此外,提示的shell脚本通常是不文明的;当没有用户提供输入时,它们当然不能可靠地使用,并且它们不能在命令的管道中可靠地使用。这严重限制了它们的用处。

因此,通用脚本不提问。特殊用途脚本可以提问。而且这取决于脚本的设计目标以及将要使用它的人员。但要尽可能避免讨厌。


你的第二个功能同样很混乱。这是不是很清楚什么是应该做的,但这个看上去比原来更加合理:

testFiles() 
{ 
    for filename in "[email protected]" 
    do 
     if [ -w "$filename" ] 
     then 
      echo "The file $filename exists and is writable"; 
      saveResults "$filename" 
     elif [ -d "$filename" ] 
     then 
      echo "The file $filename exists and is a directory". 
     elif [ -f "$filename" ] 
     then  
      echo "The file $filename exists but is not writable" 
     else 
      echo "Either $filename does not exist or it is neither" 
      echo "a file nor a directory" 
     fi 
    done 
} 

修改为每个丢失的文件提示一次一个答案 - 创建文件...

testFiles() 
{ 
    for filename in "[email protected]" 
    do 
     if [ -w "$filename" ] 
     then 
      echo "The file $filename exists and is writable"; 
      saveResults "$filename" 
     elif [ -d "$filename" ] 
     then 
      echo "The file $filename exists and is a directory". 
     elif [ -f "$filename" ] 
     then  
      echo "The file $filename exists but is not writable" 
     elif [ ! -e "$filename ] 
     then 
      echo "$filename does not exist - create it? " 
      read yesno 
      case "$yesno" in 
      ([Yy]*) cp /dev/null "$filename" 
        saveResults "$filename" 
        ;; 
      (*)  echo "OK - ignoring $filename" 
        ;; 
      esac 
     fi 
    done 
} 
+0

Minor nit:将它传递给saveResult时,您可能需要完全$ filename。 – Sorpigal 2010-12-06 15:39:02