简体   繁体   English

aws-cli 不适用于我在 bash 中创建的变量

[英]aws-cli won't work with a variable I created in bash

I'm trying to write a bash script for creating an AWS SQS queue and then check that the queue is empty.我正在尝试编写一个 bash 脚本来创建 AWS SQS 队列,然后检查队列是否为空。 For that I created the following script that should return "null":为此,我创建了以下应返回“null”的脚本:

aws sqs create-queue --queue-name MessagesQueue

queue_url=$(aws sqs list-queues --query QueueUrls[0])

queue_check=$(aws sqs get_queue_attributes --queue_url $queue_url --attribute-names ApproximateNumberOfMessages --query Attributes[0].ApproximateNumberOfMessages

And I receive this error message instead:我收到此错误消息:

An error occurred (InvalidAddress) when calling the GetQueueAttributes operation: The address "https://eu-west-3.queue.amazonaws.com/XXXXXXXXXXXX/MessagesQueue" is not valid for this endpoint.

But if I explicitly write the same address in the command instead of using $queue_url it works fine and returns 'null' as it should.但是,如果我在命令中明确写入相同的地址而不是使用 $queue_url 它工作正常并返回'null',因为它应该。

Why isn't it accepting $queue_url but accepts the same URL address if I explicitly write it?为什么它不接受 $queue_url 而是接受相同的 URL 地址,如果我明确写了它?

Edit : It seems like the aws-cli command reads the variable $queue_url as the URL between single and double-quotes, and when I write it explicitly it reads the URL with no quotes so that's why it accepts it.编辑:似乎 aws-cli 命令读取变量$queue_url作为单引号和双引号之间的 URL,当我明确写入它时,它读取不带引号的 URL,这就是它接受它的原因。 How can I use the bash variable I created so the aws-cli reads it with no quotes?如何使用我创建的 bash 变量,以便 aws-cli 不带引号读取它?

Once I tried to run your code, part by part (started with aws sqs list_queues ), I have noticed:一旦我尝试逐步运行您的代码(从 aws sqs list_queues 开始),我注意到:

aws: error: argument operation: Invalid choice, valid choices are:
...
Invalid choice: 'list_queues', maybe you meant:
  * list-queues

Try out list-queues instead list_queues and let know how it goes.尝试使用 list-queues 而不是 list_queues 并让我们知道它是如何进行的。

I solved it, I just had to remove the quotes from the output and I did it like this:我解决了它,我只需要从输出中删除引号,我是这样做的:

queue_url=$(aws sqs list-queues --query QueueUrls[0])
queue_url="${queue_url%\"}"
queue_url="${queue_url#\"}"

This other stackoverflow question helped: Shell script - remove first and last quote (") from a variable另一个stackoverflow问题有帮助: Shell script - remove first and last quote (") from a variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM