0

I use the following command db_main_info=$(aws ssm get-parameter --name "env_prod_db_main" | jq -r '.Parameter .Value') to retrieve some credential information for my db. The output looks like the following.

{ "host": "env-prod-blah-db.blah.us-east-1.rds.amazonaws.com", "dbname": "blah", "user": "root", "password": "blah" } 

Then i use the following commands to store the host,dbname,user, and password.

  db_main_host=$(echo $db_main_info | jq -r '.host')
  db_main_name=$(echo $db_main_info | jq -r '.dbname')
  db_main_user=$(echo $db_main_info | jq -r '.user')
  db_main_password=$(echo $db_main_info | jq -r '.password')

I am not very familiar with bash or jq and i am wondering if there is a better way to store the host,dbname,user,and password.

Dinero
  • 1,070
  • 2
  • 19
  • 44
  • So you have working code, but you want to retrieve it only running `jq` once instead of once per variable? Or is your question something else? (To be clear, that's very doable, and I think we have answered questions that describe it; I'm just not sure if I'm correctly understanding what it is you're asking; asking for "a better way" is a very broad question, typically too broad to be on-topic). – Charles Duffy Feb 20 '21 at 17:41
  • ...btw, there _is_ a bug in your existing code; it should be `echo "$db_main_info"` with the quotes; otherwise you're prone to the bugs described in [I just assigned a variable, but `echo $variable` prints something different!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Feb 20 '21 at 17:42
  • Yes that is right i only want to run jq once, oh wow the question is closed before i could even clarify..lol. – Dinero Feb 20 '21 at 17:48
  • Closed with a link to a duplicate that answers the question, though, right? (If you'd clarified in a different direction, I'd have reopened it; but because _without_ the clarification it was close-eligible for being overbroad or unclear, this was a case where making an interpretation that allowed close-as-duplicate was unreasonable). Anyhow -- if you have problems trying to apply the answers from the linked duplicate, feel free to edit the question to demonstrate them, and if it's well-formed and unique after those edits, I'll gladly reopen. – Charles Duffy Feb 21 '21 at 18:22

0 Answers0