Re: How to get variable out to shell script

Поиск
Список
Период
Сортировка
От Alex Gadea
Тема Re: How to get variable out to shell script
Дата
Msg-id 1304317868.50711253486561931.JavaMail.root@mail-3.01.com
обсуждение исходный текст
Ответ на Re: How to get variable out to shell script  (Sam Mason <sam@samason.me.uk>)
Ответы Re: How to get variable out to shell script  (Scott Marlowe <scott.marlowe@gmail.com>)
Список pgsql-general
Yes, I'd like to do it via Perl, but I don't have control over the server and the admins who do may balk at the idea of
loadingthe necessary db modules. 

This will work though.  Thanks!

Alex
----- Original Message -----
From: "Sam Mason" <sam@samason.me.uk>
To: pgsql-general@postgresql.org
Sent: Sunday, September 20, 2009 6:21:05 PM GMT -05:00 US/Canada Eastern
Subject: Re: [GENERAL] How to get variable out to shell script

On Sun, Sep 20, 2009 at 04:49:03PM -0500, Alex Gadea wrote:
> ie: select into ct count(*) from table;
>
> I can't figure out how to make the ct variable available to the shell
> script once the external sql file completes execution.

Just tell psql not to output any surrounding stuff and then just
redirect as normal:

  ct="`psql -tc 'select count(*) from table;'`"
  echo $ct

I expect it'll probably be easier to use a "real" scripting language
though; Python and Perl both have reasonable libraries for talking to
Postgres with.  Python would be something like:

  import psycopg2;
  conn = psycopg2.connect("dbname='db1'");
  cur = conn.cursor();
  cur.execute ("select count(*) from table;");
  [[n]] = cur.fetchall();

It's a bit of a fiddle to change over, but having a something more
expressive than a bourne shell can help.

--
  Sam  http://samason.me.uk/

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

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

Предыдущее
От: Sam Mason
Дата:
Сообщение: Re: How to get variable out to shell script
Следующее
От: Scott Marlowe
Дата:
Сообщение: Re: How to get variable out to shell script