By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

"echo"ing a psql command in a bash script

P: n/a
Hi all!
I'm writing a script that presents the user with a numbered lines
menu, each line corresponding to a <case n> which executes a psql
command. As the psql-commands are very similar to each other (all of
them have the structure:

1.- psql mydb -x -c "SELECT * FROM tb_nm WHERE col_nm LIKE '%$k_r%'" )

I thought it was possible to shorten it, initializing a str with said
command at the beginning of the script and limiting the various case
lines to defining the col_nm and the $k_r to be searched, i.e.:

2. -
(once for all):
cmnd=echo psql mydb -x -c "SELECT * FROM tb_nm WHERE $col_nm LIKE '%$k_r%'"

(in each <case n>):
col_nm="....."
echo $cmnd
---------------------------
The point is that:
when I test command "1", the query result appears immediately on the
screen and disappears (leaving room to the script menu), when I press
"q"; i.e., the environment remains that of psql until I press "q", which
seems to be a correct behaviour;

whereas, testing command "2", first appears the menu again (as if the
query had been unsuccessfull) and then (after pressing Enter - which
normally would cause exiting the script) the query result shows.

Could somebody throw some light on this issue?
Thanks for your attention.
Ennio.

--
[Perche' usare Win$ozz (dico io) se ..."anche uno sciocco sa farlo. \\?//
Fa' qualche cosa di cui non sei capace!" (diceva Henry Miller) ] (|)
[Why to use Win$ozz (I say) if ... "even a fool can do that. )=(
Do something you aren't good at!" (used to say Henry Miller) ]

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ennio-Sr said:
(once for all):
cmnd=echo psql mydb -x -c "SELECT * FROM tb_nm WHERE $col_nm LIKE '%$k_r%'"
That will be executed immediately, and give you an error.
(in each <case n>):
col_nm="....."
echo $cmnd


I'm surprised you get anything. AFAIK this should parse the files in your
directory into column names. Maybe you are doing something slightly different?

Rather than setting the $cmnd variable, try using something like this:

echo mydb -x -c \"SELECT "*" FROM tb_nm WHERE $col_nm LIKE \'%$k_r%\'\" |
xargs psql

I'm not sure where you are storing your column names, but if you can pipe it
as a list into the above you won't need to rum a loop in your script. There
are a thousand ways to do this, but most important is remember to quote the
"*" so the shell doesn't parse it. BTW You did not say, so I'm also assuming
you are using "bash" or "sh" since that's what this looks like. :-)

Best,

Jim
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.