【问题标题】:passing command from awk to the next command using xargs使用 xargs 将命令从 awk 传递到下一个命令
【发布时间】:2020-03-06 14:11:58
【问题描述】:

我正在使用 AWS EC2 CLI 对停止的实例执行筛选,然后使用从实例标签中获取的 AMI 名称从这些实例中创建一个 AMI。

aws ec2 describe-instances --output text --profile proj --query 'Reservations[*].[Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==`Name`].Value][0][0]]]'  --filter --filters Name=instance-state-name,Values=stopped | awk '{print $1, $8}' | xargs -n2 aws ec2 create-image --profile proj --instance-id {} --name {} --no-reboot

如何让args区分AWK中的两个不同参数(instnaceid,instance name tag),从而可以正确pump到ec2 create-image中的instance-id和--name参数上

【问题讨论】:

  • 您能否在问题中添加更多详细信息(例如 awk 在您的要求中的作用等),因为它不清楚。
  • awk的作用是按列过滤结果,其中第一列为instance-id,第二列为ec2实例名。
  • 我相信您的问题可以简化。假设您有一个脚本“X”(在本例中为 awk),您的问题是如何让 xargs 解析“X”的输出。您会考虑从问题中删除 awk 吗?您可以将 awk 的输出等同于输入文本文件。
  • 我相信即使在从问题中删除 awk 之后,上下文仍然是相同的。上下文仍然是关于如何使用 xargs 将参数从输入传递到下一个命令 ec2-create-image
  • @user2155404 在这里使用awk 是完全多余的。

标签: shell amazon-ec2 awk aws-cli xargs


【解决方案1】:

不需要awk。使用 AWS CLI,您首先提取 8 个值,然后使用 awk 从这 8 个值中提取 2 个值。为什么?只需从 AWS CLI 中提取 2 个值,而不使用 awk

--query 'Reservations[*].[Instances[*].[InstanceId, [Tags[?Key==`Name`].Value][0][0]]]'

将只返回您感兴趣的值。然后使用xargs 将参数传递给您的下一个命令。

xargs -n2 command --arg1 $1 --arg2 $2

你的整个命令变成:

aws ec2 describe-instances --output text --profile proj --query 'Reservations[*].[Instances[*].[InstanceId, [Tags[?Key==`Name`].Value][0][0]]]'  --filter --filters Name=instance-state-name,Values=stopped | xargs -n2 aws ec2 create-image --profile proj --instance-id $1 --name $2 --no-reboot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-06
    • 2019-03-22
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多