Re: How to get variable out to shell script

Поиск
Список
Период
Сортировка
От Sam Mason
Тема Re: How to get variable out to shell script
Дата
Msg-id 20090920222105.GE5407@samason.me.uk
обсуждение исходный текст
Ответ на How to get variable out to shell script  (Alex Gadea <alex.gadea@apptik.com>)
Ответы Re: How to get variable out to shell script  (Alex Gadea <alex.gadea@apptik.com>)
Список pgsql-general
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/

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

Предыдущее
От: Devrim GÜNDÜZ
Дата:
Сообщение: Re: How to get variable out to shell script
Следующее
От: Alex Gadea
Дата:
Сообщение: Re: How to get variable out to shell script