linux - 如何在shell脚本中使用带有某些条件的循环
问题描述
我需要curl
多次运行命令,每次都检查结果。我使用 for 循环来检查 的输出curl
,并且我想在达到正确状态时退出循环。
第一次curl
运行命令时,它会向我显示以下输出:
{
"name": "organizations/org_name/operations/long_running_operation_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.apigee.v1.OperationMetadata",
"operationType": "INSERT",
"targetResourceName": "organizations/org_name",
"state": "IN_PROGRESS"
}
}
完成需要时间,这就是为什么它说IN_PROGRESS
。大约花了 30 到 60 秒,如果我们curl
在 30 到 60 秒后运行相同的命令,那么它将显示以下输出:
{
"error": {
"code": 409,
"message": "org graceful-path-310004 already associated with another project",
"status": "ALREADY_EXISTS",
"details": [
{
"@type": "type.googleapis.com/google.rpc.RequestInfo",
"requestId": "11430211642328568827"
}
]
}
}
这是我到目前为止的脚本:
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
echo $test
while [ $test = "ALREADY EXISTS" || $test != "IN_PROGRESS" ]
do
echo $test
echo "Organization is creating"
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
done
echo "exit from loop"
解决方案
要减少重复代码,请使用函数。
此外,jq
用于生成json 的最佳实践——它将安全地处理数据中的任何嵌入引号:
json() {
jq --arg name "$ORG_NAME" \
--arg displayName "$ORG_DISPLAY_NAME" \
--arg description "$ORGANIZATION_DESCRIPTION" \
--arg runtimeType "$RUNTIMETYPE" \
--arg analyticsRegion "$ANALYTICS_REGION" \
--null-input --compact-output \
'$ARGS.named'
}
errorStatus() {
curl -H "Authorization: Bearer $TOKEN" \
-X POST \
-H "content-type:application/json" \
-d "$(json)" \
"${URL}?parent=projects/$PROJECT_ID" \
| jq -r '.error.status'
}
while true; do
status=$(errorStatus)
[ "$status" = "ALREADY EXISTS" ] && break
done
echo "exit from loop"
该while true; do some code; condition && break; done
位是do-while
循环的 bash 版本
推荐阅读
- swift - Swift:如何使用带有错误类型的标准错误?
- javascript - 如何在角度使用 index.html 文件中的动态谷歌地图 api 键?
- r - 需要帮助了解小鼠插补后缺失值如何增加
- java - 原因:升级到 Java 11 后 IntelliJ 中的 java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
- android - Kotlin 数据类 Jackson 序列化/反序列化相同对象失败
- .htaccess - 当我指定要匹配的单词时,我的 htaccess 重定向不起作用
- crash - ejabberd 不起作用,因为 erl.exe 总是崩溃
- r - 如何在 R 中读取这个特定的 .dat 文件?
- google-chrome - 如何安全地存储 Chrome 扩展程序数据?
- apache-kafka - 无法在 kafka 中列出经纪人 ID