Re: Better way to process boolean query result in shell-like situations? - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: Better way to process boolean query result in shell-like situations?
Date
Msg-id 20151029215727.GA18864@svana.org
Whole thread Raw
In response to Better way to process boolean query result in shell-like situations?  (Tim Landscheidt <tim@tim-landscheidt.de>)
List pgsql-general
On Thu, Oct 29, 2015 at 02:42:00AM +0000, Tim Landscheidt wrote:
> Hi,
>
> I regularly run into the problem that I want to query a
> PostgreSQL database in a script/program and depending on a
> boolean result do one thing or the other.  A typical example
> would be a Puppet Exec that creates a user only if it does
> not exist yet.
>
> But unfortunately psql always returns with the exit code 0
> if the query was run without errors.  In a shell script I
> can use a query that returns an empty string for failure and
> something else for success and then test that à la:


<snip>

What I do is use grep, for example (off the top of my head):

if ! psql -qAt -c "select usename from pg_user" | grep -q USERNAME ;
then
...

If you're looking for true/false you could grep for t/f.

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> The combine: one man, one day, wheat for half a million loaves of bread.

Attachment

pgsql-general by date:

Previous
From: John R Pierce
Date:
Subject: Re: Configure Different Databases on One Server
Next
From: Thomas Kellerer
Date:
Subject: Re: Configure Different Databases on One Server