Tuesday, 15 April 2014

iterate json into bash variable -


If I have a json string in a bass script, then how can I convert it to an array?

 {"name": "foo", "id": "123"} {"name": "bar", "id": "456"}  

Do I want to be prompted on the w / name, and she will tell me which ID (in one variable) I need to use

something like

  Raise your poison: 1) Foo 2) times #?  

If I select 1, then id 123 goes to the variable X, and if I select 2, then id 456 <

There is a better way to read data in an Associated Array For - here, using for parsing.

  declared - a data = () while the IFS = read -r id name; Data is named [$ id] = $ & lt; & Lt; (Jq -r '@text' \ (. Id) \ (.nim) "'< 

Given this, you Then you can handle your menu:

  echo "Choose your poison:" "$ {! Data [=]} "for ID in; name = $ {data [$ id]} printf '% d)% s \ n'" $ id "" $ name "read -p ''? 'Selection resonant' user Selected $ {data [$ selection]} " 

No comments:

Post a Comment