Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 90F9B9FB2B7 for ; Tue, 23 Jan 2007 02:04:51 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 71056-06 for ; Tue, 23 Jan 2007 02:04:46 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id A98B39FB58C for ; Tue, 23 Jan 2007 01:03:49 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from aibo.runbox.com (aibo.runbox.com [193.71.199.94]) by svr4.postgresql.org (Postfix) with ESMTP id 97E075AF90B for ; Tue, 23 Jan 2007 05:03:48 +0000 (GMT) Received: from [10.9.9.130] (helo=fenris.runbox.com) by greyhound.runbox.com with esmtp (Exim 4.50) id 1H9Doo-0001Ab-F9; Tue, 23 Jan 2007 06:03:38 +0100 Received: from mail by fenris.runbox.com with local (Exim 4.50) id 1H9Doo-0008U8-CO; Tue, 23 Jan 2007 06:03:38 +0100 Content-Type: text/plain; charset="iso-8859-1" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Received: from [24.210.132.226] by www.runbox.com with http (uid:277232) (RMM 4.0); Tue, 23 Jan 2007 05:03:38 GMT From: "Josh Williams" To: jeff@frostconsultingllc.com Cc: rray@mstc.state.ms.us, pgsql-sql@postgresql.org Subject: Re: How to query by column names Date: Tue, 23 Jan 2007 00:03:38 -0500 (EST) X-Sender: 277232 X-Mailer: RMM References: In-Reply-To: Message-Id: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.385 tagged_above=0 required=5 tests=BAYES_50, SPF_SOFTFAIL X-Spam-Level: * X-Archive-Number: 200701/111 X-Sequence-Number: 27263 From: Jeff Frost > On Mon, 22 Jan 2007, Richard Ray wrote: ... > > #!/bin/bash > > CMD=3D"psql -d test \"select * from t1\"" > > echo $CMD >> my_log > > eval $CMD | > > while read x; do > > do_something_with_x > > done > > > > In this example * expands to all files in the current working directory. > > I was attempting to get around this by enumerating the table attributes. >=20 > Oh! Why didn't you just say that in the first place. You just need quote= s.=20 ... That's definitely part of it. I'm assuming the above is an abridged exampl= e and the OP is doing something dynamic with the query. The real trouble i= s Bash likes to expand the asterisk into a list of every file in the curren= t directory when you try to push the command through a variable. So it's j= ust a matter of finding a way to escape the * character to keep Bash from g= lobbing, which unfortunately right now is escaping me (no pun intended.) Two reasonable workarounds come to mind: 1. Turn off Bash's pathname expansion: #!/bin/bash -f This will of course disable it script-wide, and thus will break any place y= ou actually are trying to use this feature, if at all. 2. Don't put an * in the variable. If all you're really doing is replacing the table name then only stick that= into a variable, say tablename, and directly execute the rest: psql -d test -c "SELECT * FROM $tablename" | while etc Worst case, you'll end up with a messy $leftside and $rightside variable se= t. To answer the original question, the field must be hard coded either as a l= ist or that perhaps over-used(?) asterisk. If you really need to pull and = use that from the table definition you'll need two round trips to the serve= r. Best of luck, - Josh Williams