Bash,来自文件的grep句子

时间:2016-05-13 08:25:17

标签: bash

我用bash制作了一个简单的电话答录机。基本上如果你问候它,它会迎接回来,但现在我的句子分析有问题。 如果句子($ @)不止一个单词,则会失败。

if [[ "$@" = $(grep -Fx "$@" 'vocabulary/greeting') ]]
        then
            speak greeting
        elif [[ "$@" = $(grep -Fx "$@" '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.
> 

我该如何解决这个问题? 我怎样才能在将来发现这些错误?

1 个答案:

答案 0 :(得分:4)

使用"$*"代替"$@"

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

"$*"是参数的字符串表示形式,而"$@"是数组。