2016-05-13 5 views
3

私はbashで簡単な留守番電話を作った。基本的に挨拶すると、挨拶しますが、文章解析に問題があります。 文($ @)が複数の単語である場合、それは失敗します。ファイルからBash、grep文

if [[ "[email protected]" = $(grep -Fx "[email protected]" 'vocabulary/greeting') ]] 
     then 
      speak greeting 
     elif [[ "[email protected]" = $(grep -Fx "[email protected]" 'vocabulary/appreciative') ]] 

出力:私はこの問題を解決するにはどうすればよい

> hello 
Sam: Hi! 
> how are you 
grep: are: No such file or directory 
grep: you: No such file or directory 
grep: are: No such file or directory 
grep: you: No such file or directory 
grep: are: No such file or directory 
grep: you: No such file or directory 
grep: are: No such file or directory 
grep: you: No such file or directory 
grep: are: No such file or directory 
grep: you: No such file or directory 
Sam: I don't understand. 
> 

? 今後どのようにこれらのエラーが発生する可能性がありますか?

答えて

4

"[email protected]"の代わりに"$*"を使用してください。

if [[ "$*" = "$(grep -Fx "$*" 'vocabulary/greeting')" ]] 

"$*""[email protected]"が配列されている間、引数の文字列表現です。

+3

これはOPで機能する可能性がありますが、この方法ではすべての空白を1つのスペースに呑み込むことに注意してください。ですから、./script How です。あなたは './scriptになるでしょうかです。おそらく、それは簡単にgrepするために、OPが望んでいるものです。 – anishsane

関連する問題