bash - `gcloud builds list` 无法在变量中捕获命令输出
问题描述
我有一些工作代码遵循下面的模式。
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 中。
其他不起作用的东西,从这里拉出来:如何将输出重定向到 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 命令似乎表现不同?
解决方案
我刚刚想通了。它打印到 STDERR 而不是 STDOUT。捕捉:
OUT=$(gcloud builds list --ongoing 2>&1)
从现在开始,我只需要使用2>&1
我所有的支票。
推荐阅读
- mysql - MySQL 查询以获取每周销售的产品的总和,使用连接从另一个表中获取平均数据
- reactjs - 为什么我找不到错误模块:无法解析“redux”
- javascript - Javascript 舍入到最接近的最大值
- java - 带有字符的 RecyclerView 分隔符
- elasticsearch - 弹性搜索 - 如何组合多个必须子句?
- model-view-controller - 有什么特别的架构,我们应该在 Flutter 移动应用程序开发中使用吗?
- java - 在指向另一个之前是否有必要关闭 PreparedStatement?
- go - golang接口如何转换数组?
- javascript - 将 contentEditable 单元格的输入类型动态更改为“数字”
- javascript - 如何在 appendchild 方法 javascript 或 reactjs 中使用文件夹路径中的 svg?