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 1evSFQ-0002U6-IX for pgsql-general@arkaria.postgresql.org; Mon, 12 Mar 2018 18:32:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evSFO-0001HM-PR for pgsql-general@arkaria.postgresql.org; Mon, 12 Mar 2018 18:32:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evSFO-0001HD-HK for pgsql-general@lists.postgresql.org; Mon, 12 Mar 2018 18:32:34 +0000 Received: from eastrmfepo101.cox.net ([68.230.241.213]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evSFK-0005Ij-PF for pgsql-general@lists.postgresql.org; Mon, 12 Mar 2018 18:32:34 +0000 Received: from eastrmimpo210.cox.net ([68.230.241.225]) by eastrmfepo101.cox.net (InterMail vM.8.01.05.28 201-2260-151-171-20160122) with ESMTP id <20180312183224.WKLS11711.eastrmfepo101.cox.net@eastrmimpo210.cox.net> for ; Mon, 12 Mar 2018 14:32:24 -0400 Received: from [192.168.1.10] ([70.171.109.83]) by eastrmimpo210.cox.net with cox id M6YQ1x00G1o04Vy016YQ3N; Mon, 12 Mar 2018 14:32:24 -0400 X-Authority-Analysis: v=2.2 cv=d+mF8VrE c=1 sm=1 tr=0 a=uCHdjGDeLqaieW9t8IbZ6A==:117 a=uCHdjGDeLqaieW9t8IbZ6A==:17 a=x7bEGLp0ZPQA:10 a=T11QR6bd6vIA:10 a=r77TgQKjGQsHNAKrUKIA:9 a=mDV3o1hIAAAA:8 a=kviXuzpPAAAA:8 a=epTmVMiNAAAA:8 a=plTPl7SvsjQDdVt7OW0A:9 a=QEXdDO2ut3YA:10 a=aUjvKglRmOkA:10 a=ILBIg42JYRoA:10 a=x6lvMIK2tO1pfIawbyMA:9 a=MiqajBr_dGCGEIRI:21 a=_W_S_7VecoQA:10 a=_FVE-zBwftR9WsbkzFJk:22 a=qrIFiuKZe2vaD64auk6j:22 a=ndEWmUVY6Yapc0oHF_P4:22 X-CM-Score: 0.00 Authentication-Results: cox.net; auth=pass (PLAIN) smtp.auth=ron.l.johnson@cox.net Subject: Re: psql in a bash function To: pgsql-general@lists.postgresql.org References: From: Ron Johnson Message-ID: <5d7d2c62-6e8a-ef53-d1d2-c48588006c48@cox.net> Date: Mon, 12 Mar 2018 13:32:24 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------0B8FC317FAF258F28315C740" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------0B8FC317FAF258F28315C740 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit I knew it was something simple... Thanks On 03/12/2018 01:24 PM, Eric Raskin wrote: > Yes, you need double quotes around $@, as in "$@". > > https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html > > > > Sent from my Verizon, Samsung Galaxy smartphone > > > -------- Original message -------- > From: Ron Johnson > Date: 3/12/18 2:15 PM (GMT-05:00) > To: pgsql-general > Subject: 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 () > { >     echo "P1=$1"; >     echo "P2=$2"; >     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.  Any ideas why the > function isn't properly passing the "-c" and '"select ..."' to psql? > > $ psql -U postgres -h XYZ -c "select oid, datname from pg_database;" >    oid   |        datname > ---------+------------------------ >        1 | template1 >    11563 | template0 >    11564 | postgres >    16404 | test1 >  3039800 | ABCD >   319011 | EFGH >   649861 | IJKL > (7 rows) > > $ psqldba -c *'*"select oid, datname from pg_database;"*'* > P1=*-c* > P2=*"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:  database "oid," does not exist > > Thanks > > -- > Angular momentum makes the world go 'round. -- Angular momentum makes the world go 'round. --------------0B8FC317FAF258F28315C740 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
I knew it was something simple...  Thanks

On 03/12/2018 01:24 PM, Eric Raskin wrote:
Yes, you need double quotes around $@, as in "$@".




Sent from my Verizon, Samsung Galaxy smartphone


-------- Original message --------
From: Ron Johnson <ron.l.johnson@cox.net>
Date: 3/12/18 2:15 PM (GMT-05:00)
Subject: 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 ()
{
    echo "P1=$1";
    echo "P2=$2";
    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.  Any ideas why the function isn't properly passing the "-c" and '"select ..."' to psql?

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

$ psqldba -c '"select oid, datname from pg_database;"'
P1=-c
P2="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:  database "oid," does not exist

Thanks

--
Angular momentum makes the world go 'round.


--
Angular momentum makes the world go 'round.
--------------0B8FC317FAF258F28315C740--