Dimaf Dimaf - 5 months ago 28
Linux Question

Postgres: Pipeline to psql

I have a simple question, but I can't find an answer )

I have following script:

sqsh -S SyBaseServer -U sybuser -P mypass -C 'select top 10000 * from dba.sybtable; -mbcp' | \
awk '{print substr($0, 0, length($0)-1)}' | \
psql -1 postgresql://admin:adminpass@pgserv:5432/pgbase -f sybtopg.sql


How can I add EOF after last string of ouput of awk? I must close STDIN for psql.

Thanks!

PS
The reason of fail isn't lack of EOF.
It's right pipeline:

sqsh -S SyBaseServer -U sybuser -P mypass -C 'select top 10000 * from dba.wybtable; -mbcp' \
| awk '{print substr($0, 0, length($0)-1)}' | \
/psql postgresql://admin:adminpass@pgserv:5432/pgbase -c "DELETE FROM testtable; COPY testtable FROM STDIN DELIMITER '|' CSV"

Answer

You don't need to do anything special. When awk reaches EOF on its input, it will execute its END block (if there is one) and then exit, and this will close the pipe. That will then cause the next process in the pipeline to read EOF.

If you want the awk script to send EOF earlier than that, it will have to exit, by using the exit statement.