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

Поиск
Список
Период
Сортировка
От Martijn van Oosterhout
Тема Re: Better way to process boolean query result in shell-like situations?
Дата
Msg-id 20151029215727.GA18864@svana.org
обсуждение исходный текст
Ответ на Better way to process boolean query result in shell-like situations?  (Tim Landscheidt <tim@tim-landscheidt.de>)
Список 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.

Вложения

В списке pgsql-general по дате отправления:

Предыдущее
От: John R Pierce
Дата:
Сообщение: Re: Configure Different Databases on One Server
Следующее
От: Thomas Kellerer
Дата:
Сообщение: Re: Configure Different Databases on One Server