gpt4 book ai didi

bash - bash if 语句中的错误 : Conditional binary operator expected

转载 作者:行者123 更新时间:2023-12-05 01:01:28 31 4
gpt4 key购买 nike

我的 bash 脚本中有一个 if 语句,如下所示:

if [[ eb status my-env-staging-worker | grep 'Green' -ne 0 ] || [ eb status my-env-staging-web | grep 'Green' -ne 0 ]]

基本上,如果第一个或第二个 eb status 命令没有字符串 Green 我想执行一些其他的东西。

但是我得到以下错误:

需要条件二元运算符状态附近的语法错误脚本返回退出代码 2

你能告诉我有什么问题吗?

最佳答案

[[[ 不用于分组。它们是独立的命令。而不是 if [[ cmd1 -ne 0 ] || [ cmd2 -ne 0 ]],省略括号和测试,只写 if cmd1 || cmd2.

if eb status my-env-staging-worker | grep -q 'Green' || eb status my-env-staging-web | grep -q 'Green'

我添加了 -q 来抑制 grep 的输出,因为您只关心返回码。

如果你想反转条件,写:

if ! eb status my-env-staging-worker | grep -q 'Green' && ! eb status my-env-staging-web | grep -q 'Green'

if ! { eb status my-env-staging-worker | grep -q 'Green' || eb status my-env-staging-web | grep -q 'Green'; }

这里你可以看到 {} 用于分组。花括号和圆括号是 bash 的分组标记。

关于bash - bash if 语句中的错误 : Conditional binary operator expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44185853/

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