繁体   English   中英

使用 bash 脚本如何检查 aws cli 生成的 output 是否为空?

[英]Using the bash script how to check if aws cli generated output is empty or not?

我需要获取未连接到aws中任何 vpc 网络的Internet Gateways

在这里,以下命令:

igws=(aws ec2 describe-internet-gateways --query 'InternetGateways[*]' --region $REGION

echo $igws

这将产生 output 如下:

  1. 对于附加的internet gatewayAttachments块不为空
[
    {
        "Attachments": [
            {
                "State": "available",
                "VpcId": "vpc-2e83a147"
            }
        ],
        "InternetGatewayId": "igw-5a61f333",
        "OwnerId": "<some-id>",
        "Tags": []
    }
]
  1. 对于分离的internet gatewayAttachments块为空
[
    {
        "Attachments": [],
        "InternetGatewayId": "igw-0c8abdc3cb147c04d",
        "OwnerId": "<some-id>",
        "Tags": [
            {
                "Key": "Name",
                "Value": "VPNTEST"
            }
        ]
    }
]

如何检查Attachment值是否为空?

我使用了以下命令,但它无法正常工作,因为它没有捕获分离的命令:

if [ -z "$igws.Attachments[]" ]; then
do
 echo "empty"
else
 echo "not empty"
fi

有人能帮我吗?

如果jq可用,请尝试:

if [[ -z $(jq '.[].Attachments[]' <<< "$igws") ]]; then
    echo "empty"
else
    echo "not empty"
fi

也许你可以试试这个:

echo $igws|tr -s '\n' ' '|tr -d ' '|tr -s ',' '\n'|grep "Attachments"|awk -F':' '{print $2}'

暂无
暂无

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

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