Shell命令在json输出中返回值

时间:2017-04-07 00:09:17

标签: shell amazon-cloudformation

如何使用shell命令返回特定值? 在下面的例子中,我想查询返回“StackStatus”的值,即“CREATE_COMPLETE”

这是命令:

aws cloudformation describe-stacks --stack-name stackname

这是输出:

{
        "Stacks": [{
            "StackId": "arn:aws:cloudformation:ap-southeast-2:64560756805470:stack/stackname/8c8e3330-9f35-1er6-902e-50fae94f3fs42",
            "Description": "Creates base IAM roles and policies for platform management",
            "Parameters": [{
                "ParameterValue": "64560756805470",
                "ParameterKey": "PlatformManagementAccount"
            }],
            "Tags": [],

            "CreationTime": "2016-10-31T06:45:02.305Z",
            "Capabilities": [
                "CAPABILITY_IAM"
            ],
            "StackName": "stackname",
            "NotificationARNs": [],
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false
        }]
    }

1 个答案:

答案 0 :(得分:2)

aws cli支持 - query 选项来获取部件。此外,您还可以使用其他命令行工具 jq 进行类似的查询。

但是在获得第一个结果的表示中:

Sample

根据上面的输出, Stacks 是一个对象数组(一个键/值),因此需要 [0] 来获取第一个元素数组,然后 .StackStatus 是此对象中包含字符串作为值的键。 - 输出文本只是将输出显示为简单的文本值而不是看起来像json的对象。

根据Charles评论编辑。