I have a command that spews json to stdout like so:

foo inf --db

    "Name": "A rose by any any other",
    "Config": {
        "DBHost": "abc.efg.us-east-1.rds.amazonaws.com",
        "DBName": "bsghshsgh",
        "DBUser": "bshshs",
        "DBPassword": "secret"

I'd like to echo the contents of DBPasswod to the screen and then execute a pgsql command with the other paramaters like so. So far I can concatenate a string to send to stdout like so:

foo inf --db | jq '"password: " + .Config.DBPassword + "; psql -h " + .Config.DBHost + " -U " + .Config.DBUser + " " + .Config.DBName'

How do I just echo the password to stdout and execute the pgsql command?


Your JSON is malformed. Also, you should probably reconsider the sanity of this approach. But if you insist to do it this way, you might try something like this:

foo inf --db | \
    jq -r '.Config | .DBPassword, .DBHost, .DBUser, .DBName' | \
        read -r PGPASSWORD
        read -r host
        read -r user
        read -r db

        psql -h "$host" -U "$user" "$db"                

(cf. libpq-envars for PGPASSWORD).

  • Well Ubuntu isn't respecting $PGPASSWORD, or any of the other variable, but otherwise this basically works. Nov 16 '16 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.