Re: How to get variable out to shell script

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to get variable out to shell script
Date: 2009-09-20 22:21:05
Message-ID: 20090920222105.GE5407@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alex Gadea 2009-09-20 22:42:41 Re: How to get variable out to shell script
Previous Message Devrim GÜNDÜZ 2009-09-20 22:10:43 Re: How to get variable out to shell script