Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evS7E-0001yv-RP for pgsql-general@arkaria.postgresql.org; Mon, 12 Mar 2018 18:24:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evS7D-0004V9-KI for pgsql-general@arkaria.postgresql.org; Mon, 12 Mar 2018 18:24:07 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evS7D-0004Uv-8p for pgsql-general@lists.postgresql.org; Mon, 12 Mar 2018 18:24:07 +0000 Received: from static-108-14-248-170.nycmny.fios.verizon.net ([108.14.248.170] helo=mail.paslists.com) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evS79-0001bR-Fz for pgsql-general@postgresql.org; Mon, 12 Mar 2018 18:24:06 +0000 Received: from mail.paslists.com (mail.paslists.com [192.168.10.96]) by mail.paslists.com (Postfix) with ESMTP id 8A7942A0168; Mon, 12 Mar 2018 14:24:01 -0400 (EDT) Received: by mail.paslists.com (kopano-spooler) with MAPI; Mon, 12 Mar 2018 14:24:01 -0400 Subject: Re: psql in a bash function From: =?utf-8?Q?Eric_Raskin?= To: =?utf-8?Q?Ron_Johnson?= , =?utf-8?Q?pgsql-general?= Date: Mon, 12 Mar 2018 18:24:01 +0000 Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="=_1nIFUteF8t7mUvN+9HFvv80VV4QhLkM2G2lwCzIRagT76OP-" X-Priority: 3 (Normal) X-Mailer: Kopano 8.5.81.353 Message-Id: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. Your mail reader does not understand MIME message format. --=_1nIFUteF8t7mUvN+9HFvv80VV4QhLkM2G2lwCzIRagT76OP- Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Yes, you need double quotes around $@, as in "$@". https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.htm= l Sent from my Verizon, Samsung Galaxy smartphone -------- Original message -------- From: Ron Johnson =20 Date: 3/12/18 2:15 PM (GMT-05:00)=20 To: pgsql-general =20 Subject: psql in a bash function=20 Hi, =20 Because I need to log into many servers, I created functions as keyboard= shortcuts (not aliases, since I will want to embed these shortcuts in ot= her functions). =20 psqlxyz () { =C2=A0=C2=A0=C2=A0 echo "P1=3D$1"; =C2=A0=C2=A0=C2=A0 echo "P2=3D$2"; =C2=A0=C2=A0=C2=A0 psql -U postgres -h XYZ $@ } =20 This is the (simple, test) command that I want to run, which works when = run explicitly using psql, but not my function.=C2=A0 Any ideas why the f= unction isn't properly passing the "-c" and '"select ..."' to psql=3F =20 $ psql -U postgres -h XYZ -c "select oid, datname from pg_database;" =C2=A0=C2=A0 oid=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= datname=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=20 ---------+------------------------ =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 1 | template1 =C2=A0=C2=A0 11563 | template0 =C2=A0=C2=A0 11564 | postgres =C2=A0=C2=A0 16404 | test1 =C2=A03039800 | ABCD =C2=A0 319011 | EFGH =C2=A0 649861 | IJKL (7 rows) =20 $ psqldba -c '"select oid, datname from pg_database;"' P1=3D-c P2=3D"select oid, datname from pg_database;" psql: warning: extra command-line argument "datname" ignored psql: warning: extra command-line argument "from" ignored psql: warning: extra command-line argument "pg_database;"" ignored psql: FATAL:=C2=A0 database "oid," does not exist =20 Thanks =20 =20 --=20 Angular momentum makes the world go 'round. --=_1nIFUteF8t7mUvN+9HFvv80VV4QhLkM2G2lwCzIRagT76OP- Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Yes, you need double quotes around $@, as i= n "$@".

https://www.gnu.org/software/bash/manual= /html_node/Special-Parameters.html



Sent from my Verizon, Samsung Galaxy smartphone=


--= ------ Original message --------
From: Ron Johnson <ron.l.jo= hnson@cox.net>
Date: 3/12/18 2:15 PM (GMT-05:00)
To: pgsql-general <pgsql-general@postgresql.org>
Subj= ect: psql in a bash function



Hi,

Because I need to log into many servers, I created functions as keyboard shortcuts (not aliases, since I will want to embed these shortcuts in other functions).

psqlxyz ()
{
=C2=A0=C2=A0=C2=A0 echo "P1=3D$1";
=C2=A0=C2=A0=C2=A0 echo "P2=3D$2";
=C2=A0=C2=A0=C2=A0 psql -U postgres -h XYZ $@
}

This is the (simple, test) command that I want to run, which works = when run explicitly using psql, but not my function.=C2=A0 Any idea= s why the function isn't properly passing the "-c" and '"select ..."' to psql=3F

$ psql -U postgres -h XYZ -c "select oid, datname from pg_database;"
=C2=A0=C2=A0 oid=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 datname=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 <= br> ---------+------------------------
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 1 | template1<= br> =C2=A0=C2=A0 11563 | template0
=C2=A0=C2=A0 11564 | postgres
=C2=A0=C2=A0 16404 | test1
=C2=A03039800 | ABCD
=C2=A0 319011 | EFGH
=C2=A0 649861 | IJKL
(7 rows)

$ psqldba -c '"= select oid, datname from pg_database;"'
P1=3D-c
P2=3D"select oid, datname from pg_database;"
psql: warning: extra command-line argument "datname" ignored
psql: warning: extra command-line argument "from" ignored
psql: warning: extra command-line argument "pg_database;"" ignored
psql: FATAL:=C2=A0 database "oid," does not exist

Thanks

--
Angular momentum makes the world go 'round.
--=_1nIFUteF8t7mUvN+9HFvv80VV4QhLkM2G2lwCzIRagT76OP---