JSON 配置到 bash 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7795171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON config to bash variables
提问by Jeffrey04
Possible Duplicate:
Unix command-line JSON parser?
可能重复:
Unix 命令行 JSON 解析器?
If I have a config file in JSON and a PHP script to flatten the config file into something like this
如果我有一个 JSON 格式的配置文件和一个 PHP 脚本来将配置文件压缩成这样
database_dbname=sensei
database_password=somerandompassword
memcached_host=localhost
....
can I pipe this to my bash script and make each of the entry above as a variable?
我可以将它通过管道传输到我的 bash 脚本并将上面的每个条目作为变量吗?
./bin/flatten_config.php config.json | ./bin/my_bash_script.sh
so that in my bash-script I can use values from the config file
以便在我的 bash 脚本中我可以使用配置文件中的值
mysql -D${database_dbname} -p${database_password} ...
采纳答案by don_jones
In bashyou could write in your script-file
在bash你可以在你的脚本文件的写入
source <(./bin/flatten_config.php config.json)
bashwill take the output of flatten_config.phpand parse it like input
bash将获取输出flatten_config.php并像输入一样解析它

