我正在尝试检查命令的输出作为Bash中even循环的一个分离条件,但是它一直跳过块,即使最后一个回显确认值为"DELETE_IN_PROGRESS"。
这就是我所拥有的:
stackname=cf_test
while [[ $(aws cloudformation describe-stacks --stack-name ${stackname} | jq '.Stacks | .[0] | .StackStatus') == "DELETE_IN_PROGRESS" ]]; do
echo -e " $(aws cloudformation describe-stacks --stack-name ${stackname} | jq '.Stacks | .[0] | .StackStatus'): waiting for current stack to delete before re-deploying..."
sleep 30
done
echo -e $(aws cloudformation describe-stacks --stack-name ${stackname} | jq '.Stacks | .[0] | .StackStatus')我该换什么?
编辑:
添加-x调试标志,将提供:
▶ bash -x ~/Downloads/test_script.sh
+ stackname=cf_test
++ aws cloudformation describe-stacks --stack-name cf_test
++ jq '.Stacks | .[0] | .StackStatus'
+ [[ "DELETE_IN_PROGRESS" == \D\E\L\E\T\E\_\I\N\_\P\R\O\G\R\E\S\S ]]
++ aws cloudformation describe-stacks --stack-name cf_test
++ jq '.Stacks | .[0] | .StackStatus'
+ echo -e '"DELETE_IN_PROGRESS"'
"DELETE_IN_PROGRESS"发布于 2019-10-26 02:22:22
调试日志显示了问题(尽管[[行本身奇怪地模糊了问题):
+ echo -e '"DELETE_IN_PROGRESS"'您正在比较的值包含文字双引号。这就是为什么比赛失败的原因。==的右侧包含不作为字符串一部分的语法双引号。
要修复它,使用jq -r输出没有JSON格式和转义的字符串:
json='{ "foo": "bar" }'
jq '.foo' <<< "$json" # Shows bad 5 character value: "bar"
jq -r '.foo' <<< "$json" # Shows good 3 character value: bar就你而言:
while [[ $(aws cloudformation describe-stacks --stack-name ${stackname} | jq -r '.Stacks | .[0] | .StackStatus') == "DELETE_IN_PROGRESS" ]]; doq -r '.Stacks | .[0] | .StackStatus') == "DELETE_IN_PROGRESS" ]]; do
echo "Still deleting"
sleep 30
done发布于 2019-10-25 19:45:45
我会将比较移到jq内部。默认情况下,无论进行比较的逻辑状态如何,jq都会成功。
% jq -n '3 == 3'; echo $?
true
0
% jq -n '3 != 3'; echo $?
false
0若要更改此选项,请使用-e选项。
% jq -en '3 == 3'; echo $?
true
0
% jq -en '3 != 3'; echo $?
false
1这样做就不需要[[ ... ]]命令了。
while x=$(aws cloudformation describe-stacks --stack-name ${stackname});
jq -ne --arg x "$x" '$x.Stacks.[0].StackStatus == "DELETE_IN_PROGRESS"'; do
printf '%s: waiting for current stack to delete before re-deploying...\n' "$x"
sleep 30
done
printf '%s\n' "$x"https://stackoverflow.com/questions/58564414
复制相似问题