我有这个命令:aws ec2 describe-security-groups | jq '.SecurityGroups[]| "(.GroupId)"'
我想将stdout存储到bash变量中。
主要目标是:运行for循环以遍历存储在此变量中的每个元素。
所以我做了这个:
我想将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 help
我的假设是,结果变量应该以这种方式存储其元素:
"sg-01a"
"sg-0c2"
"sg-4bf"
但我不确定我的假设是否正确。
result=$(aws ec2 describe-security-groups --output text --query 'SecurityGroups[*].GroupId')
。要遍历列表,请使用:for val in $result; do echo $val ; done
。 - krishna_mee2004