bash - 使用 curl 命令上的“jq”输出作为 while 循环中的条件
问题描述
我希望此语句在此 api 响应中的 json 字段的值为 null 时继续循环。
while $(curl --location --request GET "https://example.com/integration-test/results/json"| jq '.result') == null
do
echo "Waiting for Integration tests to finish. Trying again in 10 seconds."
sleep 2
done
显然不起作用,== null
但它说明了我的目标。当集成测试完成时,此 api 调用将在“结果”中返回 SUCCESS 或 FAILURE。那是我希望循环停止的时候。
解决方案
考虑使用-e
参数使其退出状态反映其输出(当唯一的输出是orjq
时发出失败的退出状态)。 null
false
为了清晰而不是简洁而编写下面的代码(使用显式分组运算符来明确否定适用于整个管道):
#!/usr/bin/env bash
set -o pipefail # make a failure on the left-hand side fail the entire pipeline
while ! { curl --fail -L "https://example.com/integration-test/results/json" \
| jq -e '.result' >/dev/null; }; do
echo "Waiting for Integration tests to finish. Trying again in 10 seconds."
sleep 10
done
推荐阅读
- php - JWT/LARAVEL 令牌过期
- c# - 在块调度中使用 jQuery 合并 html 表格单元格
- android - 将 google_maps_flutter 颤动到现有项目中
- ruby-on-rails - 设计添加到用户,当用户未登录网页时直接进入rails中的登录页面
- python - 如何在 AWS API 网关中返回非默认回复而不引发异常?
- sql - 选择具有特定模式的表名
- google-apps-script - 如何使用 Google Script 获取 Google Drive 中文件的所有驱动器位置?
- javascript - React Bootstrap:元素类型无效
- c++ - 如何使用文件夹路径和cmake“链接所有”动态和/或静态库?
- angular - Util 模块中 util.isNullOrUndefined(object) 的替代方法是什么?角 7