gpt4 book ai didi

Bash,grep文件中的句子

转载 作者:行者123 更新时间:2023-11-29 09:25:36 25 4
gpt4 key购买 nike

我用 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.
>

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

最佳答案

使用 "$*" 而不是 "$@"

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

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

关于Bash,grep文件中的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com