building shell command piece by piece
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 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: Quote:
Can someone tell me what I'm doing wrong? |
Hi,
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 |
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 |
Hi,
Basic mysql from script using a HERE document: Code:
#!/bin/bash Hope this helps. |
Hi again,
Took a better look at your example and came up with this code snippet (HERE document part only): Code:
fieldOne="first" - 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. Hope this clears things up some more. |
Okay, I finally got back to getting this running, and it's handling the arguments gloriously.
But for some reason, it's stripping the table ascii (the "|"s, "-"s, etc.) from the output, which makes it difficult to read. So, I naturally thought to run the output through awk, and my thought was to do this: Code:
mysql --user="user2" --password="foopass" --database="footable" <<MYSQL |
Hi,
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. Hope this gets you going again. |
That was it. Figures it would be something in mysql. I very rarely use the mysql command line, and it shows. :P
Thanks for all your help! |
All times are GMT -5. The time now is 09:47 PM. |