How to I loop through AWS CLI output?

无人久伴 提交于 2021-02-11 14:44:13

问题


I am trying to pass a list of values from a aws cli query to another command. Even though I have seen plenty of examples in AWS, when I try all the values come together:

  policy_versions=`aws iam list-policy-versions --query "Versions[].VersionId" --policy-arn $POLICY_ARN --output text`
  echo "policy_versions=$policy_versions"
  for ver in $policy_versions; do
    echo "first version: $ver"
  done

Which then prints out:

policy_versions=v3  v2  v1
first version: v3   v2  v1

My value of ver is the entire string, it should be v3 then v2 and v1. But It is instead: v3 v2 v1.

I cannot figure out what is wrong.


回答1:


I was able to reproduce your results and I traced the results to a difference between shell and zsh (which is now used as the default shell in MacOS).

Put simply, you can run this command to make it work the way you expect: setopt shwordsplit

For more details, see: iteration - Shell script - iterate over space separated words/characters (in zsh) - Stack Overflow



来源:https://stackoverflow.com/questions/62593695/how-to-i-loop-through-aws-cli-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!