【发布时间】:2017-01-05 08:40:14
【问题描述】:
这里是volumes.json:
{
"Volumes": [
{
"AvailabilityZone": "us-east-1a",
"Tags": [
{
"Value": "vol-rescue-system",
"Key": "Name"
}
],
"VolumeId": "vol-00112233",
},
{
"AvailabilityZone": "us-east-1a",
"Tags": [
{
"Value": "vol-rescue-swap",
"Key": "Name"
}
],
"VolumeId": "vol-00112234",
},
{
"AvailabilityZone": "us-east-1a",
"Tags": [
{
"Value": "vol-rescue-storage",
"Key": "Name"
}
],
"VolumeId": "vol-00112235",
}
]
}
我需要同时获取VolumeId 和Tags.Value 的值以用作调用另一个命令的输入。从 json 数组中获取单个值很容易,但我无法从中提取 multiple value 并将其传递给另一个 bash 命令。
我可以使用这个得到一个值:
cat volumes.json |jq -r '.Volumes[].VolumeId' |while read v; do another_bash_command $v; done
但我无法获得多个值,因为这是错误的:
cat volumes.json |jq -r '.Volumes[].VolumeId, .Volumes[].Tags[].Value' |while read v w; do another_bash_command $v $w; done
因为它会循环 6 次而不是 3 次。
并且,如何将循环中的多个 json 值传递给 bash 数组,以便更好地使用该值?比如VolumeId-> $arr[0][0]、Tags.Value-> $arr[0][1]、AvailabilityZone-> $arr[0][2]...等。我搜索了 SO 和 jq 文档,并尝试了readarray,但仍然无法找到解决方案:(感谢您提供的任何帮助。
【问题讨论】: