#
#------------------------------------------------------------------------------
# usage example:
# doExportJsonSectionVars cnf/env/dev.env.json '.env.virtual.docker.spark_base'
#------------------------------------------------------------------------------
doExportJsonSectionVars(){
json_file="$1"
shift 1;
test -f "$json_file" || echo "the json_file: $json_file does not exist !!! Nothing to do" && exit 1
section="$1"
test -z "$section" && echo "the section in doExportJsonSectionVars is empty !!! nothing to do !!!" && exit 1
shift 1;
while read -r l ; do
eval $l ;
done < <(cat "$json_file"| jq -r "$section"'|keys_unsorted[] as $key|"export \($key)=\(.[$key])"')
}
只需管道即可
keys
运行:样品
input.json
:输出:
一个更通用的 bash 函数来导出变量(带插值):
示例数据