On 23 October 2017 at 15:08, Mark Lybarger <mlybarger@gmail.com> wrote:
> I have this bash/sql script which outputs some curl commands. the backticks
> causes it to get interpreted by the shell. This works fine if there is one
> result, but when there are many rows returned, it looks like one shell
> command.
>
> any help on getting multiple rows returned to be executed by the shell would
> be appreciated!
I tend to do
psql ..... | while read a; do
# some code
done
The only problem I find with this is that you can't pass variables out
of the while loop, because the pipe runs as a subshell. You could of
course use echo and encapsulate the whole thing, eg this would take
your results and (assuming they're integers) return the largest -
obviously your own code could decide differently how to output things.
You also have to move the read to later on, so you can send your
output to the parent.
myres=$( a=0 biga=0 psql -tqAX -h ${DB_HOST} -d ${DB_NAME} -u ${DB_USER} -c "select
'curl -X POST http://${REGISTER_HOST}:8080/' || source_id || '/${MT}/'
|| model || '/' || site || '/backoffice/register' from
myschema.events where source_id = $SOURCE_ID and ineffective_date is
null" | while true; do if [ $a -gt $biga ] ; then biga=$a fi if ! read a; then echo $biga; break; fi done
)
A mess, but it works.
To be honest, by the time you've got to this level of complexity you
probably shouldn't be using shellscript any more.
Geoff
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general