Search Postgresql Archives

Re: How to get variable out to shell script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux