无法将aws cli命令存储到bash中的变量中

无法将aws cli命令存储到bash中的变量中,bash,amazon-web-services,asynchronous,aws-cli,Bash,Amazon Web Services,Asynchronous,Aws Cli,问题是 我有一个脚本,检查是否配置了AWS凭据,然后获取配置的区域并创建VPC 这是: #!/usr/bin/env bash if [ -z "$(aws configure get aws_access_key_id)" ]; then echo "AWS credentials not configured. Aborting."; exit 1; fi; export REGION=$(aws configure get region) export vpcId=$

问题是

我有一个脚本,检查是否配置了AWS凭据,然后获取配置的区域并创建VPC

这是:

#!/usr/bin/env bash

if [ -z "$(aws configure get aws_access_key_id)" ]; then
    echo "AWS credentials not configured. Aborting.";
    exit 1;
fi;

export REGION=$(aws configure get region)

export vpcId=$(aws --region "$REGION" ec2 create-vpc --cidr-block 10.0.0.0/24 --query 'Vpc.VpcId' --output text)
问题是,
$REGION
为空,即使直接从控制台执行
aws configure get REGION
会返回以下内容:
us-west-1
。在脚本中,它不返回任何内容

另一件奇怪的事是:

导出vpcId=$(aws ec2创建vpc--cidr块10.0.0.0/24--查询“vpc.vpcId”--输出文本)
返回vpc ID,并将其成功存储在
vpcId
变量中

这有什么问题:
导出区域=$(aws配置获取区域)
。是否存在异步I/O(
aws configure get
从配置文件读取,
aws ec2创建vpc
从internet读取)


这是从一开始的整个脚本:

#!/usr/bin/env bash

# Test availability of aws-cli
hash aws 2>/dev/null
if [ $? -ne 0 ]; then
    echo >&2 "'aws' command line tool required, but not installed. Aborting.";
    exit 1;
fi;

# Test availability of the AWS AccessKey
if [ -z "$(aws configure get aws_access_key_id)" ]; then
    echo "AWS credentials not configured. Aborting.";
    exit 1;
fi;

# Directory
export EC2_STARTER_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export WORKING_DIR="$( pwd )"

# File
export AWS_CONFIG_FILE="${WORKING_DIR}/.aws"
CREATE_VPC="${EC2_STARTER_DIR}/setup/create_vpc.sh"

# Defaults
export REGION="$( aws configure get region )" # Empty variable
#!/usr/bin/env bash

aws configure get region # Output us-west-1

# Test availability of aws-cli
hash aws 2>/dev/null
if [ $? -ne 0 ]; then
    echo >&2 "'aws' command line tool required, but not installed. Aborting.";
    exit 1;
fi;

aws configure get region # Output us-west-1

# Test availability of the AWS AccessKey
if [ -z "$(aws configure get aws_access_key_id)" ]; then
    echo "AWS credentials not configured. Aborting.";
    exit 1;
fi;

aws configure get region # Output us-west-1

# Directory
export EC2_STARTER_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export WORKING_DIR="$( pwd )"

aws configure get region # Output us-west-1

# File
export AWS_CONFIG_FILE="${WORKING_DIR}/.aws"
CREATE_VPC="${EC2_STARTER_DIR}/setup/create_vpc.sh"

aws configure get region # No output

尝试寻找bash脚本的源代码,因为export-inside将把所有的内容导出到子shell中

source your_script 
或者你也可以试试这个:

 ./your_script  # Same as source command

这是从一开始的整个脚本:

#!/usr/bin/env bash

# Test availability of aws-cli
hash aws 2>/dev/null
if [ $? -ne 0 ]; then
    echo >&2 "'aws' command line tool required, but not installed. Aborting.";
    exit 1;
fi;

# Test availability of the AWS AccessKey
if [ -z "$(aws configure get aws_access_key_id)" ]; then
    echo "AWS credentials not configured. Aborting.";
    exit 1;
fi;

# Directory
export EC2_STARTER_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export WORKING_DIR="$( pwd )"

# File
export AWS_CONFIG_FILE="${WORKING_DIR}/.aws"
CREATE_VPC="${EC2_STARTER_DIR}/setup/create_vpc.sh"

# Defaults
export REGION="$( aws configure get region )" # Empty variable
#!/usr/bin/env bash

aws configure get region # Output us-west-1

# Test availability of aws-cli
hash aws 2>/dev/null
if [ $? -ne 0 ]; then
    echo >&2 "'aws' command line tool required, but not installed. Aborting.";
    exit 1;
fi;

aws configure get region # Output us-west-1

# Test availability of the AWS AccessKey
if [ -z "$(aws configure get aws_access_key_id)" ]; then
    echo "AWS credentials not configured. Aborting.";
    exit 1;
fi;

aws configure get region # Output us-west-1

# Directory
export EC2_STARTER_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export WORKING_DIR="$( pwd )"

aws configure get region # Output us-west-1

# File
export AWS_CONFIG_FILE="${WORKING_DIR}/.aws"
CREATE_VPC="${EC2_STARTER_DIR}/setup/create_vpc.sh"

aws configure get region # No output
原来,
AWS\u CONFIG\u FILE
是AWS用来设置配置文件路径的环境变量,AWS配置获取区域命令从该文件读取。因此,下面的行
export AWS\u CONFIG\u FILE=“${WORKING\u DIR}/.AWS”
只是覆盖了它。(谢谢@123)


很抱歉问了这么愚蠢的问题。调试时,我并不认为错误是一个简单的变量名冲突,我认为这是由于其他原因造成的。我的错误…

将stderr写入stdout。
$(aws配置获取区域2>&1)
正确吗?我还尝试了
$(echo$(aws配置获取区域)2>&1)
。如果您在脚本中运行命令,但未将其分配给变量,则它们都不起作用。它是否输出到屏幕?它是否与导出区域=
aws配置获取区域
(狗屎引号,只需使用简单的“)@123感谢提示,完全有意义-同意
$()
更实用,将更新我的脚本。