If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Running query from batch file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-27-11, 17:52
steezburgers steezburgers is offline
Registered User
 
Join Date: Apr 2011
Location: Madison, WI
Posts: 11
Running query from batch file

Hello,

I have a MySQL 5.1.56 server running on Linux.

I want to be use the command line tool to run some queries in a .sql file and then output the results to a text file.

When I run my command from Terminal it works as expected, but when I run the same thing from a batch file it doesn't. The output text file is created, but nothing is written to it.

My batch file is as follows:

#!/bin/bash
mysql -uUSER -pPASSWORD db_name < /path/to/file.sql > /path/to/output.txt

For testing purposes my .sql file only contains a simple SELECT statement.
Like I said, when I run this:

mysql -uUSER -pPASSWORD db_name < /path/to/file.sql > /path/to/output.txt

from Terminal it works fine and output.txt contains the output of the SELECT statement. The batch file will create output.txt but there isn't anything in it... What am I doing wrong??

Thanks for the help!
Reply With Quote
  #2 (permalink)  
Old 12-28-11, 13:21
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
When you login in Linux it automatically executes /etc/profile which sets up some environment variables. If you have a local version of $HOME/.profile (may differ slightly based on shell you are using) this also gets called which may setup other environment variables (especially the PATH).

There are two options either setup the PATH environment variable to point to the folder containing the mysql command or put in the full pathname to the mysql command in your script.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 12-28-11, 15:00
steezburgers steezburgers is offline
Registered User
 
Join Date: Apr 2011
Location: Madison, WI
Posts: 11
I believe I do have the full path to the sql file. I have /home/Bitnami/Desktop/output.txt for the path.

Either way I believe the path is correct because the file is created, it's just not populated with any data.
Reply With Quote
  #4 (permalink)  
Old 12-29-11, 13:55
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
I think you should reread my post. I said full pathname to the mysql command. You have:

mysql -uUSER -pPASSWORD db_name < /path/to/file.sql > /path/to/output.txt

and I am saying

/path/to/mysql -uUSER -pPASSWORD db_name < /path/to/file.sql > /path/to/output.txt
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 12-29-11, 15:08
steezburgers steezburgers is offline
Registered User
 
Join Date: Apr 2011
Location: Madison, WI
Posts: 11
Oh! Derp!

That did it! Thanks so much for the help!!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On