帮助阅读 yml(解析器代码):https://gist.github.com/pkuczynski/8665367
我的 YML 文件是:
marketVal:
envVal:
hostVal: ipValue
在 .sh 文件中,我将其读为
market $1
env $2
parse_yaml() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
eval $(parse_yaml hostList.yml "config_")
#Here my property key is "config_marketValue_envValue_hostVal"
# so the value of "config_marketValue_envValue_hostVal" is properly coming as "ipValue"
echo ${config_marketValue_envValue_hostVal}
#As I can not hard code the marketVal etc so the key will be like
# "config_${market}_${env}_hostVal"
现在,如果我在脚本中做这样的事情:
key="config_${market}_${env}_hostVal"
echo ${key}
ip_value=${key}
echo ${ip_value}
正在印刷。
config_${market}_${env}_hostVal *nothing get printed for echo ${ip_value}*
而预期输出是:
config_marketValue_envValue_hostVal
ipValue
任何人都可以建议在这里做什么来读取这个值,我对 shell 脚本非常陌生,任何帮助将不胜感激。