Order of execution in shell echo to psql

Started by John Gagealmost 16 years ago4 messagesgeneral
Jump to latest
#1John Gage
jsmgage@numericable.fr

I issue the following command to the shell:

echo '\o file.txt \\ select * from table_name limit 10 \o ' | psql --
host 'localhost' --port 5432 --username 'johngage' 'database_name'

I expect the results to be redirected to file.txt because that is the
meta-command immediately preceeding the query.

In fact, the results go to stdout, obviously on account of the \o at
the end of the query. And, equally obviously, I can leave that out
(which "works").

But I am surprised that the \o is the controlling meta-command.

Thanks,

John

#2David W Noon
dwnoon@ntlworld.com
In reply to: John Gage (#1)
Re: Order of execution in shell echo to psql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, 2 May 2010 14:13:52 +0200, John Gage wrote abour [GENERAL]
Order of execution in shell echo to psql:

I issue the following command to the shell:

echo '\o file.txt \\ select * from table_name limit 10 \o ' | psql --
host 'localhost' --port 5432 --username 'johngage' 'database_name'

Try putting a semi-colon at the end of your SQL query. [Or try using
proper copy-and-paste when writing your messages.]
- --
Regards,

Dave [RLU #314465]
======================================================================
dwnoon@ntlworld.com (David W Noon)
======================================================================
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (GNU/Linux)

iEYEARECAAYFAkvdeiwACgkQRQ2Fs59Psv/1FQCgw+m+tBm+5VtnMPFrMn+q6xnV
gwMAoNIHaCb3vVSyu35w8aJ9IZfmKV6g
=vnpA
-----END PGP SIGNATURE-----

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: David W Noon (#2)
Re: Order of execution in shell echo to psql

David W Noon <dwnoon@ntlworld.com> writes:

On Sun, 2 May 2010 14:13:52 +0200, John Gage wrote abour [GENERAL]

I issue the following command to the shell:

echo '\o file.txt \\ select * from table_name limit 10 \o ' | psql --
host 'localhost' --port 5432 --username 'johngage' 'database_name'

Try putting a semi-colon at the end of your SQL query.

Or put a \g there. As it stands, execution of the SQL query is
triggered by the EOF at the end of the string. So the fact that the
second \o got executed before that is unsurprising.

regards, tom lane

#4John Gage
jsmgage@numericable.fr
In reply to: Tom Lane (#3)
Re: Order of execution in shell echo to psql

Thanks very much for elucidating this. \g is going to help me in this
situation more than the ;

John

On May 2, 2010, at 4:25 PM, Tom Lane wrote:

Show quoted text

David W Noon <dwnoon@ntlworld.com> writes:

On Sun, 2 May 2010 14:13:52 +0200, John Gage wrote abour [GENERAL]

I issue the following command to the shell:

echo '\o file.txt \\ select * from table_name limit 10 \o ' |
psql --
host 'localhost' --port 5432 --username 'johngage' 'database_name'

Try putting a semi-colon at the end of your SQL query.

Or put a \g there. As it stands, execution of the SQL query is
triggered by the EOF at the end of the string. So the fact that the
second \o got executed before that is unsurprising.

regards, tom lane

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