2010-12-06 8 views
0

私はこの実行を助ける必要があります。ちょうど2つのセクション。固定する必要がある2つのbash関数

私は初心者のbashスクリプトです。 1つのファイル名が入力されるまでこれをどのように繰り返すのですか? PSそれdoens'tは今まで私が試したwhileループで動作するようです。

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

コードに字下げを使用してください。あなたが書いたものの頭や尾を作るのに役立ちます。 –

答えて

3

ifSpaces()機能には、より良い定義が必要です - それは何をする必要がありますか?

  • 考えられるファイル名を1つとります。
  • この引数に空白が含まれていない場合は、それを返します。
  • この引数にスペースが含まれている場合は、スペースを含まない名前が指定されるまで新しいファイル名の入力を求めます。

文字列(別名ファイル)の空白をテストするにはどうすればよいですか?

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 
} 

OK - それが行われ、今、私たちは、 "なぜ?" 尋ねます。すべての主要なオペレーティングシステム(ファイルシステム)が有効なものとしてスペースを含むファイル名を認識するので、あなたのコードが有効なファイル名で動作することを確認するほうが良いのではないでしょうか?

さらに、プロンプトを表示するシェルスクリプトは、しばしば非公開です。入力を提供するユーザーがいない場合でも確実に使用することはできず、コマンドのパイプラインでは確実に使用することはできません。それはその有用性を厳しく制限します。

したがって、汎用スクリプトは質問しません。特別な目的のスクリプトが質問をすることができます。そして、それはスクリプトが何をするように設計されているのか、誰がそれを使うのかに依存します。しかし、可能なときはいつも嫌なことは避けてください。


2番目の機能も同様に非常に混乱します。行うことになっているものは非常に明確ではないが、これは元よりもっともらしいです:答えを一度欠落しているファイルごとに要求するように改訂

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

+0

@Sorpigal:かなり - '' $ filename "'を引用するべきです。私は実際にはそれをより多くの場所に引用するでしょう。 '$ filename 'が複数の単語に対して(例えば、テスト演算子の中で)1つの単語に展開されたときに採用された規則(シェルでの引用を学んだ198x以降の新しい規則)は理解できません。だから、ブルートフォース・アンド・セーフティのアプローチでは、毎回$ filenameが二重引用符で囲まれます... –

+0

ファイルが存在しない場合にファイルを作成しようとする部分を行う必要がある場合、どのようにこのファイルを追加しますか?このコードはかなり変わっていますか? – cyberian

関連する問題