【问题标题】:xargs not splitting on whitespace from aws cli outputxargs 没有从 aws cli 输出中拆分空格
【发布时间】:2020-10-11 05:54:04
【问题描述】:

此命令返回由空格分隔的所有 AWS 区域:

aws ec2 describe-regions --query 'Regions[*].RegionName' --output text

eu-north-1  ap-south-1  eu-west-3   eu-west-2   eu-west-1   ap-northeast-2  ap-northeast-1  sa-east-1   ca-central-1    ap-southeast-1  ap-southeast-2  eu-central-1    us-east-1   us-east-2   us-west-1   us-west-2

我正在尝试将其通过管道传输到 xargs,但它会将其视为单个字符串:

aws ec2 describe-regions --query 'Regions[*].RegionName' --output text | gxargs -I {} aws cloudformation list-stacks --region {}

Invalid endpoint: https://cloudformation.eu-north-1 ap-south-1  eu-west-3   eu-west-2   eu-west-1   ap-northeast-2  ap-northeast-1  sa-east-1   ca-central-1    ap-southeast-1  ap-southeast-2  eu-central-1    us-east-1   us-east-2   us-west-1   us-west-2.amazonaws.com
gxargs: aws: exited with status 255; aborting

gxargs 只是 gnu xargs(我在 Mac 上)。

另外,尝试使用 jmespath 从具有特定分隔符的数组创建字符串(我可以与 xargs 一起使用):

aws ec2 describe-regions --query 'Regions[*].join(",",@.RegionName)'
In function join(), invalid type for value: None, expected one of: ['string'], received: "null"

编辑:只是跟进,这就是我的结论。它坚持在找不到堆栈时抛出错误 - 可能与其他 aws cli 命令相同

aws ec2 describe-regions --query 'Regions[*].RegionName' --output text | gxargs -n 1 sh -c 'aws cloudformation describe-stacks --stack-name findme --region $0 || true'

【问题讨论】:

    标签: bash amazon-web-services aws-cli xargs jmespath


    【解决方案1】:

    这里是man xargs -I

    -I replace-str
    用从标准输入读取的名称替换初始参数中出现的 replace-str。此外,未加引号的空格 不要终止输入项;相反,分隔符是换行符。隐含 -x 和 -L 1。

    您可以改用xargs -n 1 aws cloudformation list-stacks --region

    【讨论】:

    • 为什么-n 1 有效? -n 的手册页让我很困惑,我能理解它在做什么
    • 一次只运行一个区域的命令,因为我想无论你如何拆分它们都无法运行多个区域
    • 我知道我的意思是 xargs 是如何解析它的?我认为默认情况下 xargs 按空格拆分并为每个项目运行一次。 -n 1 表示使用空格作为分隔符?
    • 没有。 -I 表示“停止使用空格作为分隔符”。 -n 不影响分隔符,所以这里默认使用空格。
    • 有没有办法像使用 -I MYWORD 一样使用空格作为分隔符并获得对单词的引用?
    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2020-01-05
    • 1970-01-01
    相关资源
    最近更新 更多