jq key,每个shell脚本的值

时间:2017-09-12 13:18:45

标签: shell amazon-web-services foreach jq

我试图使用输出的每个Key,Value并将其传递给另一个命令。 以下是我尝试使用的内容:

INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID"

使用上面的命令,我有以下输出:

{
"Tags": [
    {
        "ResourceType": "instance",
        "ResourceId": "i-0342a609edf80001a",
        "Value": "A-VALUE",
        "Key": "A-KEY"
    },
    {
        "ResourceType": "instance",
        "ResourceId": "i-0342a609edf80001a",
        "Value": "B-VALUE",
        "Key": "B-KEY"
    },
    {
        "ResourceType": "instance",
        "ResourceId": "i-0342a609edf80001a",
        "Value": "C-VALUE",
        "Key": "C-KEY"
    },
    {
        "ResourceType": "instance",
        "ResourceId": "i-0342a609edf80001a",
        "Value": "D-VALUE",
        "Key": "D-KEY"
    },
    {
        "ResourceType": "instance",
        "ResourceId": "i-0342a609edf80001a",
        "Value": "E-VALUE",
        "Key": "E-KEY"
    },
    {
        "ResourceType": "instance",
        "ResourceId": "i-0342a609edf80001a",
        "Value": "F-VALUE",
        "Key": "G-KEY"
    },
    {

现在我想将每个Key,Value传递给以下命令:

aws ec2 create-tags --resources XXXXX --tags Key=H-KEY,Value=H-VALUE

Key,Value的数量和值是可变的。所以我相信我需要一个"为每个"。

你可以帮帮我吗?

它就像:对于每个Key,Value,执行:

aws ec2 create-tags --resources XXXXX --tags Key=A-KEY,Value=A-VALUE
aws ec2 create-tags --resources XXXXX --tags Key=B-KEY,Value=B-VALUE
aws ec2 create-tags --resources XXXXX --tags Key=C-KEY,Value=C-VALUE
aws ec2 create-tags --resources XXXXX --tags Key=N...-KEY,Value=N...-VALUE

1 个答案:

答案 0 :(得分:4)

有一个@sh directive输出正确引用shell的值:

In [2460]: df.sort_values('date').groupby('item').apply(change).sort_index()
Out[2460]:
        date item  price  % change previousdate
0 2015-12-31    A    110      10.0   2014-12-31
1 2015-12-31    B    120      20.0   2013-06-24
2 2014-12-31    A    100       NaN          NaT
3 2013-06-24    B    100       NaN          NaT

根据您的输入,此输出

aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" \
| jq -r '.Tags[] | @sh "aws ec2 create-tags --resources XXXXX --tags Key=\(.Key),Value=\(.Value)"' 

执行命令管道进入aws ec2 create-tags --resources XXXXX --tags Key='A-KEY',Value='A-VALUE' aws ec2 create-tags --resources XXXXX --tags Key='B-KEY',Value='B-VALUE' aws ec2 create-tags --resources XXXXX --tags Key='C-KEY',Value='C-VALUE' aws ec2 create-tags --resources XXXXX --tags Key='D-KEY',Value='D-VALUE' aws ec2 create-tags --resources XXXXX --tags Key='E-KEY',Value='E-VALUE' aws ec2 create-tags --resources XXXXX --tags Key='G-KEY',Value='F-VALUE'

sh

jq是一次冒险。您需要添加“选择”过滤器以删除以“aws:”

开头的键
aws ec2 describe-tags ... | jq -r ... | sh