-bash:[:@:预期为二进制运算符 [英] -bash: [: @: binary operator expected

查看:83
本文介绍了-bash:[:@:预期为二进制运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我正在最后一行中检查文件中的错误信息,如果出现错误,我希望结果为是 。我的Shell脚本如下所示:

I am checking the error info in a file in the last line, I would like to have a "yes" result if there is an "Error". My shell script is like below:

[ $(tail -1 error.log | grep -E "Error") ] && echo "yes"

然后我在标题中得到了上面的错误:

then I got the error like above in the title:

-bash: [: @: binary operator expected

最后一行中的错误消息如下:

The error message in the last line is like below:

[aaa,bbb,ccc, Error.ddd @ ]

我认为这是因为错误消息的格式为[@]内容导致此错误。但是我不知道如何解决它。有谁知道如何处理此[@]问题。非常感谢

I think that is because of the the Error message, which has [ @ ] format content caused this error. But I do not know how to fix it. Is there any one knows how to process this [ @ ] problem. Thanks very much

@csiu,非常感谢您的快速回复。

@csiu, thanks very much for your quick reply.

此处的窍门是使用双 [,如下所示:

The trick here is to use double "[" as below:

 [[ $(tail -1 error.log | grep -E "Error") ]] && echo "yes"


推荐答案

除了@csiu的答案,不要完全不需要 test 命令。您可以根据grep的退出状态进行操作:

Additionally to @csiu's answer, don't need the test command at all. You can operate based on grep's exit status:

tail -1 error.log | grep -qE "Error" && echo yes

使用 -q 静音grep的输出。效率更高,因为一旦找到模式,grep就会立即退出。

Use -q to silence the output from grep. It's also more efficient because grep will exit immediately once the pattern is found.

由于我们只有一行输入,我们甚至不需要grep:

Since we only have one line of input, we don't even need grep:

[[ $(tail -1 error.log) == *Error* ]] && echo yes

这篇关于-bash:[:@:预期为二进制运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆