簡體   English   中英

xargs 不會從 aws cli 輸出中拆分空白

[英]xargs not splitting on whitespace from aws cli output

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

這是-Iman xargs

-I replace-str
用從標准輸入讀取的名稱替換初始參數中出現的 replace-str。 此外,未加引號的空格不會終止輸入項 相反,分隔符是換行符。 意味着 -x 和 -L 1。

您可以使用xargs -n 1 aws cloudformation list-stacks --region代替

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM