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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:58:06  来源:igfitidea点击:

JSON config to bash variables

jsonbashconfiguration

提问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并像输入一样解析它

回答by CoolAJ86

Checkout TickTick.

结帐TickTick

It's a true Bash JSON parser.

这是一个真正的 Bash JSON 解析器。

#!/bin/bash
. /path/to/ticktick.sh

# File
DATA=`cat data.json`
# cURL
#DATA=`curl http://foobar3000.com/echo/request.json`

tickParse "$DATA"

echo ``pathname``
echo ``headers["user-agent"]``