在 bash 脚本中将变量传递给 aws cli 命令

发布于 2025-01-20 00:22:34 字数 621 浏览 0 评论 0原文

我是编写Bash脚本的新手,并且在将变量传递到AWS CLI命令方面遇到了麻烦。我敢肯定,这很容易解决,而且我只是很难理解这种语法。以下是我到目前为止的脚本:

#!/bin/bash

CONFIG_RECORDER=`aws configservice describe-configuration-recorders
export CONFIG_RECORDER_NAME=$(jq -r '.ConfigurationRecorders[].name' <<<"$CONFIG_RECORDER")

sudo yum -y install jq

aws configservice delete-configuration-recorder --configuration-recorder-name $CONFIG_RECORDER_NAME

此脚本的目的是使用AWS CLI命令删除配置记录仪。我遇到了一个错误,aws:错误:参数 - configuration-recorder-name:预期一个参数

我有一个变量,$ config_recorder_name,被指定为参数但是由于某种原因,它没有阅读。有关如何将此变量传递给此命令的任何建议将有所帮助。

I'm new to writing bash scripts and am having trouble with passing in variable to an AWS CLI command. I'm sure there's an easy fix for this, and I'm just having trouble understanding this syntax. Below is the script I have so far:

#!/bin/bash

CONFIG_RECORDER=`aws configservice describe-configuration-recorders
export CONFIG_RECORDER_NAME=$(jq -r '.ConfigurationRecorders[].name' <<<"$CONFIG_RECORDER")

sudo yum -y install jq

aws configservice delete-configuration-recorder --configuration-recorder-name $CONFIG_RECORDER_NAME

The purpose of this script is to delete the configuration recorder using the aws cli command. I'm getting an error that aws: error: argument --configuration-recorder-name: expected one argument

I have a variable, $CONFIG_RECORDER_NAME, being specified as the argument but for some reason it is not reading. Any advice on how to pass in this variable to this command would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何止钟意 2025-01-27 00:22:34

如果 aws configservice describe-configuration-recorders 调用返回单个配置记录器名称,以下内容似乎可以正常工作:

#!/bin/bash

sudo yum -y install jq

CONFIG_RECORDER=`aws configservice describe-configuration-recorders`
# echo "CONFIG_RECORDER="$CONFIG_RECORDER

CONFIG_RECORDER_NAME=$(jq -r '.ConfigurationRecorders[].name' <<<"$CONFIG_RECORDER")
# echo "CONFIG_RECORDER_NAME="$CONFIG_RECORDER_NAME

aws configservice \
    delete-configuration-recorder \
    --configuration-recorder-name $CONFIG_RECORDER_NAME

实际上不需要使用 jq。您可以简单地使用:

#!/bin/bash

CONFIG_RECORDER_NAME=`aws configservice describe-configuration-recorders --query 'ConfigurationRecorders[0].name' --output text`

aws configservice \
    delete-configuration-recorder \
    --configuration-recorder-name $CONFIG_RECORDER_NAME

The following seems to work correctly if the aws configservice describe-configuration-recorders invocation returns a single configuration recorder name:

#!/bin/bash

sudo yum -y install jq

CONFIG_RECORDER=`aws configservice describe-configuration-recorders`
# echo "CONFIG_RECORDER="$CONFIG_RECORDER

CONFIG_RECORDER_NAME=$(jq -r '.ConfigurationRecorders[].name' <<<"$CONFIG_RECORDER")
# echo "CONFIG_RECORDER_NAME="$CONFIG_RECORDER_NAME

aws configservice \
    delete-configuration-recorder \
    --configuration-recorder-name $CONFIG_RECORDER_NAME

There's actually no need to use jq. You could simply use:

#!/bin/bash

CONFIG_RECORDER_NAME=`aws configservice describe-configuration-recorders --query 'ConfigurationRecorders[0].name' --output text`

aws configservice \
    delete-configuration-recorder \
    --configuration-recorder-name $CONFIG_RECORDER_NAME
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文