noober
noober

Reputation: 1505

bash script - how to do something based on the result of a psql query

Need help trying to parse these results from within a unix shell script. I'm looking to grab the result of 1 and the logic would be: if 1 exists then proceed with rest of the code else exit.

Here's the command line string that runs my query followed by the results in STDOUT.

/bin/psql -h localhost -p 5432 -U mas postgres -c "SELECT 1 as result from pg_database WHERE datname='mydbname'";
Password for user mas:
  result
---------
       1
(1 row)

What I need help is contructing the if-then-else logic with some string parser so I can get just 1 and compare against that. How to do that?

Upvotes: 1

Views: 132

Answers (1)

that other guy
that other guy

Reputation: 123570

man psql suggests that you can use --tuples-only to just get the value, avoiding the need to parse.

Upvotes: 1

Related Questions