简体   繁体   English

提取aws命令的output,并使用jq赋值给bash中的一个变量

[英]Extracting output of aws command and assign it to a variable in bash using jq

Following aws command outputs the below json from which I want to extract the resources which is having the string "vm-managed-itg* values ( last two entries in the output)按照 aws 命令输出以下 json,我想从中提取具有字符串“vm-managed-itg* 值(输出中的最后两个条目)的资源

$aws resourcegroupstaggingapi get-resources --resource-type-filters cloudwatch:alarm --profile acc | $aws resourcegroupstaggingapi get-resources --resource-type-filters cloudwatch:alarm --profile acc | jq '.ResourceTagMappingList[] | jq '.ResourceTagMappingList[] | select(contains({Tags: [{Key: "vm:cost:accountenv"} ]}) | not) |选择(包含({标签:[{Key:“vm:cost:accountenv”}]})|不)| select(contains({Tags: [{Value: "itg"} ]}) | not)'选择(包含({标签:[{值:“itg”}]})|不)'

{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-uat-test",
  "Tags": []
}
{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-stg-test",
  "Tags": []
}
{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-uat-test3",
  "Tags": []
}
{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-stg-test1",
  "Tags": []
}
{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-itg-test1",
  "Tags": []
}
{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-itg-test",
  "Tags": []
}

I tried adding jq -r '.[].ResourceARN' but it didn't work and instead threw this error我尝试添加 jq -r '.[].ResourceARN' 但它没有用,而是抛出了这个错误

~$ aws resourcegroupstaggingapi get-resources --resource-type-filters cloudwatch:alarm --profile per-acc --output json | jq '.ResourceTagMappingList[] | select(contains({Tags: [{Key: "vm:cost:accountenv"} ]}) | not) | select(contains({Tags: [{Value: "itg"} ]}) | not)' | jq -r '.[].ResourceARN'
jq: error (at <stdin>:4): Cannot index string with string "ResourceARN"
jq: error (at <stdin>:8): Cannot index string with string "ResourceARN"
jq: error (at <stdin>:12): Cannot index string with string "ResourceARN"
jq: error (at <stdin>:16): Cannot index string with string "ResourceARN"
jq: error (at <stdin>:20): Cannot index string with string "ResourceARN"
jq: error (at <stdin>:24): Cannot index string with string "ResourceARN"

Use select to return entities only if they match a condition.使用select仅在匹配条件时返回实体。 Substring matches can be tested with contains : Substring 匹配项可以用contains进行测试:

select(.ResourceARN|contains("vm-managed-itg"))

Output: Output:

{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-itg-test1",
  "Tags": []
}
{
  "ResourceARN": "arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-itg-test",
  "Tags": []
}

If you are only interested in the values of ResourceARN , without tags and surrounding objects, then:如果您只对ResourceARN的值感兴趣,没有标签和周围的对象,那么:

.ResourceARN | select(contains("vm-managed-itg"))

jq applies the filter to every single JSON entity in its input (which is a stream of objects). jq 将过滤器应用于其输入中的每个 JSON 实体(这是一个 stream 的对象)。

Output: Output:

"arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-itg-test1"
"arn:aws:cloudwatch:us-east-1:xxxx:alarm:vm-managed-itg-test"

( jq -r for raw output without quotes). jq -r对于原始 output 不带引号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM