Re: How to get variable out to shell script

From: "Brent Wood" <b(dot)wood(at)niwa(dot)co(dot)nz>
To: <alex(dot)gadea(at)apptik(dot)com>, <devrim(at)gunduz(dot)org>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to get variable out to shell script
Date: 2009-09-20 23:41:38
Message-ID: 4AB766720200007B0001E510@gwia.niwa.co.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, 2009-09-20 at 16:49 -0500, Alex Gadea wrote:
> I am using psql to call an external sql file that executes a simple
> select count(*):
>
> 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.

Hi Alex,

If you are using bash, for example:

COUNT=`psql -d <database> -Atc "select count(*) from table;"`

in a shell script, any string enclosed in back-quotes (`) is executed & the result is returned. So the above expression assigns the value returned by the psql command to the variable called COUNT. The -Atc tells psql to return only the unaligned value, no formatting or column names, etc.

If you store your SQL command outside the script, then you could use:

COUNT=`psql -d <database> -Atf <SQL_file>`

HTH,

Brent Wood

Brent Wood
DBA/GIS consultant
NIWA, Wellington
New Zealand
NIWA is the trading name of the National Institute of Water & Atmospheric Research Ltd.

Browse pgsql-general by date

  From Date Subject
Next Message Scott Ribe 2009-09-20 23:47:39 Re: How can I make a two arch libpq for snow leopard?
Previous Message Scott Marlowe 2009-09-20 23:23:30 Re: How to get variable out to shell script