我有这个命令:aws ec2 describe-security-groups | jq '.SecurityGroups[]| “(.GroupId)”'
我希望将标准输出存储到 bash 中的变量中。
主要目标:运行 for 循环来遍历存储在该变量上的每个元素。
所以我这样做了:
#!/bin/bash
result=$(aws ec2 describe-security-groups | jq '.SecurityGroups[]| "\(.GroupId)"'))
for val in "${result[@]}"; do
aws ec2 some command $result
done
Run Code Online (Sandbox Code Playgroud)
看起来 bash 正在将我的变量的内容解释为字符串,因为我在 for 中的命令并没有安静地正确获取结果:
“SG-01A” “SG-0C2” “SG-4BF”
用法:aws [选项] [ ...] [参数] 要查看帮助文本,您可以运行:
亚马逊帮助
我的假设是结果的 var 应该以这种方式存储它的元素:
“SG-01A”
“SG-0C2”
“SG-4BF”
但不确定我的假设是否正确。