Re: How to get variable out to shell script - Mailing list pgsql-general

From Sam Mason
Subject Re: How to get variable out to shell script
Date
Msg-id 20090920222105.GE5407@samason.me.uk
Whole thread Raw
In response to How to get variable out to shell script  (Alex Gadea <alex.gadea@apptik.com>)
Responses Re: How to get variable out to shell script  (Alex Gadea <alex.gadea@apptik.com>)
List 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 by date:

Previous
From: Devrim GÜNDÜZ
Date:
Subject: Re: How to get variable out to shell script
Next
From: Alex Gadea
Date:
Subject: Re: How to get variable out to shell script