我正在创建一个 Dockerfile,它调用一个名为start.sh
“我想获取从用户传递的环境变量”的脚本,docker-compose.yml
或者docker run -e
现在我想做的是我有这些变量的列表在数组中动态定义。
## Environment Variables
## Missing Vars off the hop SLACK_HOOK_URL
CONFIG_SETTINGS=(
AWS_ID
AWS_SECRET
BUCKET_REGION
BUCKET_NAME
DB_USER
DB_PASSWORD
DB_HOSTIP
DB_PORT
DB_NAME
)
我只需取消设置此数组中定义的上述变量的子集。
CONFIG_SECRETS=(
AWS_ID
AWS_ID_STR
AWS_SECRET
AWS_SECRET_STR
BUCKET_NAME
BUCKET_NAME_STR
DB_USER
DB_USER_STR
DB_PASSWORD
DB_PASSWORD_STR
DB_HOSTIP
DB_HOSTIP_STR
DB_PORT
DB_PORT_STR
DB_NAME
DB_NAME_STR
)
我的问题是以下 for 循环。
## Sanitization section
for i in "${!CONFIG_SECRETS[@]}"; do
## Nullify environment variables that contain secrets before launching.
# echo ${CONFIG_SECRETS[$i]}
eval $"${CONFIG_SECRETS[$i]}"=$""
unset $"${CONFIG_SECRETS[$i]}"
echo \$${CONFIG_SECRETS[$i]} is now to: $"${CONFIG_SECRETS[$i]}"
done
例如,下面是我创建的一个 for 循环,用于迭代这些变量并设置它们。
for i in "${!CONFIG_SETTINGS[@]}"; do
echo ${CONFIG_SETTINGS[$i]}"_KEY"
## Indirect references http://tldp.org/LDP/abs/html/ivr.html
eval FROM_STRING=\$"${CONFIG_SETTINGS[$i]}_STR"
eval VALUE_STRING=\$${CONFIG_SETTINGS[$i]}
eval KEY_STRING=\$"${CONFIG_SETTINGS[$i]}_KEY"
TO_STRING="$KEY_STRING$VALUE_STRING"
sed -i '' "s/$FROM_STRING/$TO_STRING/g" ./config.tmpl
done
这会修改以下配置文件。
{
"aws_id": "YOUR-AWS-ID",
"aws_secret": "YOUR-AWS-SECRET",
"bucket_region": "YOUR-BUCKET-REGION",
"bucket_name": "YOUR-BUCKET-NAME",
"db_conn": "USER:PASSWORD@tcp(localhost:3306)/DBNAME",
"slack_hook_url": ""
}
完整的上下文脚本减去了本问题范围之外的内容。
#!/bin/bash
## Launch service will tell prism-bin what mode to run in.
LAUNCHMODE="${MODE:-$1}"
## This variable will be what can override default launch args. I may modify this as I learn more about prism-bin
LAUNCHARGS="${CUSTOM_ARGS:-$2}"
## This is setup this way to handle any situations that might arise from the
## config being JSON and bash not being any good at JSON.
# ## Strings to replace.
AWS_ID_STR="YOUR-AWS-ID"
AWS_SECRET_STR="YOUR-AWS-SECRET"
BUCKET_REGION_STR="YOUR-BUCKET-REGION"
BUCKET_NAME_STR="YOUR-BUCKET-NAME"
DB_USER_STR="USER"
DB_PASSWORD_STR="PASSWORD"
DB_HOSTIP_STR="localhost"
DB_PORT_STR="3306"
DB_NAME_STR="DBNAME"
# Environment Variables/Defaults
## Json sucks in BASH/Shell so you need to add trailing commas intermittently.
## Just pay attention to this. Also at some point I'll need to make a fringe
## case for handling key/values that aren't included in the default config.
AWS_ID="${AWS_ID:-potato}"
AWS_SECRET="${AWS_SECRET:-potato}"
BUCKET_REGION="${BUCKET_REGION:-potato}"
BUCKET_NAME="${BUCKET_NAME:-potato}"
DB_USER="${DB_USER:-potato}"
DB_PASSWORD="${DB_PASSWORD:-potato}"
DB_HOSTIP="${DB_HOSTIP:-potato}"
DB_PORT="${DB_PORT:-potato}"
DB_NAME="${DB_NAME:-potato}"
## Environment Variables in Array
CONFIG_SETTINGS=(
AWS_ID
AWS_SECRET
BUCKET_REGION
BUCKET_NAME
DB_USER
DB_PASSWORD
DB_HOSTIP
DB_PORT
DB_NAME
)
CONFIG_SECRETS=(
AWS_ID
AWS_ID_STR
AWS_SECRET
AWS_SECRET_STR
BUCKET_NAME
BUCKET_NAME_STR
DB_USER
DB_USER_STR
DB_PASSWORD
DB_PASSWORD_STR
DB_HOSTIP
DB_HOSTIP_STR
DB_PORT
DB_PORT_STR
DB_NAME
DB_NAME_STR
)
## Sanitization section
# Awaiting someone smarter than me to suggest a method for this.
# https://unix.stackexchange.com/questions/474097/i-want-to-unset-a-list-of-bash-variables-that-have-their-variable-strings-stored
for i in "${CONFIG_SECRETS[@]}"; do
unset $i
eval echo \$"${CONFIG_SECRETS[$i]}"=\$${CONFIG_SETTINGS[$i]}
done
答案1
这应该是一个简单的循环
#!/bin/bash
UNSET=(a b c)
a=10 b=20 c=30 d=40 e=50
echo Before a=$a b=$b c=$c d=$d e=$e
for i in ${UNSET[@]}
do
unset $i
done
echo After a=$a b=$b c=$c d=$d e=$e
这导致
Before a=10 b=20 c=30 d=40 e=50
After a= b= c= d=40 e=50
答案2
您可以使用以下方式取消引用变量!
:
$ foo=bar
$ bar=1
$ echo ${!foo}
1
你不能使用变量里面不过,像这样的扩展,所以你可能不得不用 做一些非常丑陋的事情eval
。
答案3
我认为你已经把自己陷入了一个圈子;只需循环数组并取消设置循环引用的参数即可:
AWS_SECRET='secret_here'
echo Before:
declare -p AWS_SECRET
set|grep AWS_SECRET
for secret in "${CONFIG_SECRETS[@]}"
do
unset $secret
done
echo After:
declare -p AWS_SECRET
set|grep AWS_SECRET