Re: Better way to process boolean query result in shell-like situations?

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Better way to process boolean query result in shell-like situations?
Date: 2015-10-29 21:57:27
Message-ID: 20151029215727.GA18864@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)svana(dot)org> http://svana.org/kleptog/
> The combine: one man, one day, wheat for half a million loaves of bread.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2015-10-29 22:01:33 Re: Configure Different Databases on One Server
Previous Message John R Pierce 2015-10-29 21:55:59 Re: Configure Different Databases on One Server