Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON config to bash variables [duplicate]

Possible Duplicate:
Unix command-line JSON parser?

If I have a config file in JSON and a PHP script to flatten the config file into something like this

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?

./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

mysql -D${database_dbname} -p${database_password} ...
like image 337
Jeffrey04 Avatar asked Oct 17 '11 14:10

Jeffrey04


2 Answers

In bash you could write in your script-file

source <(./bin/flatten_config.php config.json)

bash will take the output of flatten_config.php and parse it like input

like image 134
don_jones Avatar answered Sep 23 '22 14:09

don_jones


Checkout TickTick.

It's a true Bash JSON parser.

#!/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"]``
like image 36
coolaj86 Avatar answered Sep 26 '22 14:09

coolaj86