【问题标题】:`gcloud builds list` can't capture command output in variable [duplicate]`gcloud builds list`无法捕获变量中的命令输出[重复]
【发布时间】:2021-09-03 12:52:34
【问题描述】:

我有一些遵循以下模式的工作代码。

FIRST_BILLING_ACCOUNT=$(gcloud beta billing accounts list --filter=open=true --format="value(name)" --limit=1)

检查输出:$FIRST_BILLING_ACCOUNT

打印我的结算帐户名称。

像这样:

REGION=$(gcloud app describe --format="value(locationId.scope())")

检查输出:$REGION

打印区域。

问题

这不起作用:

OUT=$(gcloud builds list --ongoing)

它打印输出:Listed 0 items. 并且不将值保存在 $OUT 中。

其他不起作用的东西,从这里拉出来:How do I redirect output to a variable in shell?

OUT="$(gcloud builds list --ongoing)"

gcloud builds list --ongoing | read OUT

read OUT < <(gcloud builds list --ongoing)

我什至试过这个:

echo "gcloud builds list --ongoing" >> tst.sh
chmod +x tst.sh
OUT=$(./tst.sh)
$OUT

结果是一样的。

我想知道两件事:如何捕获此命令的输出? 为什么不同的 gcloud 命令的行为似乎不同?

【问题讨论】:

    标签: bash gcloud


    【解决方案1】:

    我刚刚想通了。它打印到 STDERR 而不是 STDOUT。捕获:

    OUT=$(gcloud builds list --ongoing 2>&1)

    从现在开始,我只需要在所有支票中使用2>&1

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 2014-04-18
      • 2016-09-04
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多