From: | Jeff Frost <jeff(at)frostconsultingllc(dot)com> |
---|---|
To: | Josh Williams <drykath(at)runbox(dot)com> |
Cc: | rray(at)mstc(dot)state(dot)ms(dot)us, pgsql-sql(at)postgresql(dot)org |
Subject: | Re: How to query by column names |
Date: | 2007-01-23 05:29:17 |
Message-ID: | Pine.LNX.4.64.0701222115190.5992@discord.home.frostconsultingllc.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
On Tue, 23 Jan 2007, Josh Williams wrote:
> From: Jeff Frost <jeff(at)frostconsultingllc(dot)com>
>> On Mon, 22 Jan 2007, Richard Ray wrote:
> ...
>
> That's definitely part of it. I'm assuming the above is an abridged example
> and the OP is doing something dynamic with the query. The real trouble is
> Bash likes to expand the asterisk into a list of every file in the current
> directory when you try to push the command through a variable. So it's just
> a matter of finding a way to escape the * character to keep Bash from
> globbing, which unfortunately right now is escaping me (no pun intended.)
Oh...you just need to put quotes around the variable like this:
#!/bin/bash
CMD="psql -c 'select * from products;' jefftest"
echo "$CMD" >> my_log
eval "$CMD" |
while read x; do
echo $x
done
discord:~ $ /tmp/test.sh
productid | name | price
-----------+-----------+-------
1 | Notepad | 1.99
3 | Legal Pad | 2.99
(2 rows)
discord:~ $ cat my_log
psql -c 'select * from products;' jefftest
At any rate, if that's still a problem, you can turn off globbing, do what
needs doing, then turn on globbing like the following:
#!/bin/bash
#
# Turn off globbing
#
set -o noglob
# ...
# Do your * laden work here
# ...
# Turn on globbing
set +o noglob
>
> 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 you 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 set.
>
> To answer the original question, the field must be hard coded either as a list 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 server.
--
Jeff Frost, Owner <jeff(at)frostconsultingllc(dot)com>
Frost Consulting, LLC http://www.frostconsultingllc.com/
Phone: 650-780-7908 FAX: 650-649-1954
From | Date | Subject | |
---|---|---|---|
Next Message | Achilleas Mantzios | 2007-01-23 08:52:28 | Re: server process (PID xxx) was terminated by signal 7 |
Previous Message | Josh Williams | 2007-01-23 05:03:38 | Re: How to query by column names |