Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to put together a shell script to query a mysql database, with the WHERE section being based on the shell arguments. It seemed like the easiest thing to do would be to make a string variable of the full mysql command, piece by piece, and then execute it at the end of the script.
Well, it didn't work. So I tried it with other commands. Same story. To prevent overcomplicating what is probably a simple problem of ignorance, I'll use my second test in the example below:
Code:
#!/bin/sh
CMD="grep";
if [ -z "$1" ]
then
CMD="$CMD"" 'foobar'";
else
CMD="$CMD"" '$1'";
fi
CMD="$CMD"" /var/log/somerandom.log";
$CMD;
exit 1;
There's easier ways to accomplish the function of this example, I know. But it was the easist way to build my mysql query. I think what I'm trying to do is pretty well conveyed.
I learned about 'sh -x' this morning, so I thought I'd try it. I'm not sure if this helps or not, but here it is:
If I 'echo "$CMD;"', and then copy/paste the command, it works great, both in the test and the mysql script. But when I execute $CMD, the mysql script outputs the 'mysql --help' info, and the grep script just takes me back to the prompt.
There are probably better ways to solve your original problem (executing a mysql statement from within a script), HERE documents being the first that comes to mind.
But, you can do it the way you tried, only thing you did wrong was the quoting:
Code:
#!/bin/sh
CMD="grep"
if [ -z "$1" ]
then
CMD="$CMD foobar"
else
CMD="$CMD $1"
fi
CMD="$CMD /var/log/somerandom.log"
$CMD
exit 0
Hmmm, then I guess my grep test doesn't demonstrate the same problem. Maybe I just need to approach this with a different method.
While I google for 'HERE documents', here's the basic gist of what I was trying to do. If you can give me a quick example of how I could do this better, that would be sweet.
Code:
# initiate the $CMD variable
CMD="mysql --user=\"usernam3\" --password=\"pa55word\" --database=\"testdb\" --execute=\"SELECT * from footable where";
LIMIT=15;
# now loop through the arguments and add the appropriate wheres
while [ -n "$1" ]; do
case $1 in
-l | --limit)
if [ -z "$2" ]
then
missing;
shift;
else
LIMIT="$2";
shift 2;
fi;;
--field1)
if [ -z "$2" ]
then
missing;
shift;
else
AND="$AND field1 like '%$2%' and ";
shift 2;
fi;;
--field2)
if [ -z "$2" ]
then
missing;
shift;
else
AND="$AND field2 like '%$2%' and ";
shift 2;
fi;;
##### there's several more of these field1/field2 things #####
-*) invalid;;
*) break;;
esac
done
# add the wheres to the $CMD variable and finish it
CMD="$CMD $AND id is not null order by date desc LIMIT $LIMIT\";";
# execute it
$CMD;
exit 1;
#!/bin/bash
some_table="footable"
other_table="handable"
token="somestring"
sortorder="whatever"
mysql <<HERE
<usernaem>/<passwd>
select * from ${some_table}
where ${other_table} like '${token}'
order by ${sortoder};
quit
exit
HERE
You can set variables outside the HERE document and use them inside the HERE document.
Took a better look at your example and came up with this code snippet (HERE document part only):
Code:
fieldOne="first"
fieldTwo="last"
limit="15"
mysql --user=\"usernam3\" --password=\"pa55word\" --database=\"testdb\" <<MYSQL
select *
from footable
where field1 like ${fieldOne}
and field2 like ${fieldTwo}
and id is not null
order by date desc
LIMIT ${limit};
MYSQL
A few points:
- You could place the username/password inside the HERE document, but you don't have to.
- fieldOne, fieldTwo and limit need to be checked/set before starting the mysql command. I hardcoded them to simplify things.
It's been a while I used mysql, but one of these two does what you want (cannot remember which ).
mysql -t
or
mysql -vvv
Normally mysql only shows table layout when using it interactively (from the mysql prompt). All other output is given without the table layout. The -t (or -vvv) options tells mysql to to the table layout even if not interactive.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.