xargs不在AWS CLI输出的空格上拆分

问题描述

此命令返回所有由空格分隔的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'

解决方法

这里是man xargs的{​​{1}}:

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

您可以改用-I replace-str