Re: How to get variable out to shell script

From: Alex Gadea <alex(dot)gadea(at)apptik(dot)com>
To: Sam Mason <sam(at)samason(dot)me(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to get variable out to shell script
Date: 2009-09-20 22:42:41
Message-ID: 1304317868.50711253486561931.JavaMail.root@mail-3.01.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 loading the necessary db modules.

This will work though. Thanks!

Alex
----- Original Message -----
From: "Sam Mason" <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)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/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2009-09-20 23:23:30 Re: How to get variable out to shell script
Previous Message Sam Mason 2009-09-20 22:21:05 Re: How to get variable out to shell script