Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>   http://svana.org/kleptog/
> The combine: one man, one day, wheat for half a million loaves of bread.

Attachment: signature.asc
Description: Digital signature


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux