我有此命令:aws ec2 describe-security-groups | jq'.SecurityGroups [] | “(.GroupId)”'
我希望将stdout存储到bash中的变量中。
主要目标:运行for循环以遍历存储在此变量上的每个元素。
所以我做到了:
#!/bin/bash
result=$(aws ec2 describe-security-groups | jq '.SecurityGroups[]| "\(.GroupId)"'))
for val in "${result[@]}"; do
aws ec2 some command $result
done
好像bash会将变量的内容解释为字符串,因为for里面的命令并不是安静地正确获取结果:
“ sg-01a”“ sg-0c2”“ sg-4bf”
用法:aws [选项] [...] [参数] 要查看帮助文本,可以运行:
aws帮助
我的假设是结果的var应该以这种方式存储其元素:
“ sg-01a”
“ sg-0c2”
“ sg-4bf”
但不确定我的假设是否正确。