Bash,文件中的grep语句
我用bash做了一个简单的答录机。基本上,如果你问候它,它会回敬你,但现在我在句子分析方面遇到了问题。 如果句子($@)不止一个单词,则它将失败Bash,文件中的grep语句,bash,Bash,我用bash做了一个简单的答录机。基本上,如果你问候它,它会回敬你,但现在我在句子分析方面遇到了问题。 如果句子($@)不止一个单词,则它将失败 if [[ "$@" = $(grep -Fx "$@" 'vocabulary/greeting') ]] then speak greeting elif [[ "$@" = $(grep -Fx "$@" 'vocabulary/appreciative') ]] 输出: > hel
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.
>
我该如何解决这个问题?
我怎样才能捕捉到将来可能出现的此类错误?使用“$*”
而不是“$@”
“$*”
是参数的字符串表示形式,而“$@”
是一个数组。虽然这可以用于OP,但请注意,这种方法会将所有空白占用为一个空格。因此,/script Howareyou
将成为/script Howareyou
。也许这正是OP想要的,为了更轻松地表现。
if [[ "$*" = "$(grep -Fx "$*" 'vocabulary/greeting')" ]]