LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-28-2011, 02:12 PM   #1
nmuleski
LQ Newbie
 
Registered: Mar 2011
Location: Madison, WI
Distribution: Ubuntu 10.10, CentOS 5.5
Posts: 11

Rep: Reputation: 0
Running batch file does not produce same results as running command from Terminal


Hello,

I am using Ubuntu Server 4.4.5 with lightweight GNOME installed. Also using MySQL 5.1.56 as well.

I want to be use the MySQL 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


When I run the command from Terminal it works fine and the output file is created and populated with data.

When I run the batch file it creates the output file but it isn't populated with the results from my SQL statement... What am I doing wrong??

Thanks for the help!
 
Old 12-28-2011, 03:42 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,889

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
This is from the MySQL manual:
Quote:
--table, -t

Display output in table format. This is the default for interactive use, but can
be used to produce table output in batch mode.
And this is from the mysql man page:
Quote:
If you want to get the interactive output format in batch mode, use mysql -t.
 
Old 12-28-2011, 04:41 PM   #3
nmuleski
LQ Newbie
 
Registered: Mar 2011
Location: Madison, WI
Distribution: Ubuntu 10.10, CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: 0
Hi and thanks for the response!

The -t switch really only affects formatting as far as I can tell. I tried adding both -t and --table to the batch file with no success.
 
Old 12-28-2011, 05:32 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
There is something that you do differently in the bash script ?

Like use some variables ? If yes, did you define them correctly ?
 
Old 12-28-2011, 05:54 PM   #5
jefro
Moderator
 
Registered: Mar 2008
Posts: 16,674

Rep: Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454Reputation: 2454
Wonder if it is the dash or dash dash at #!/bin/bash


Try

#!/bin/bash --
 
Old 12-29-2011, 10:52 AM   #6
nmuleski
LQ Newbie
 
Registered: Mar 2011
Location: Madison, WI
Distribution: Ubuntu 10.10, CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Cedrik View Post
There is something that you do differently in the bash script ?

Like use some variables ? If yes, did you define them correctly ?
I took the line right out of the batch file and ran it in Terminal. So the only difference between the two is the #!/bin/bash as the top.

---------- Post added 12-29-11 at 10:53 AM ----------

Quote:
Originally Posted by jefro View Post
Wonder if it is the dash or dash dash at #!/bin/bash


Try

#!/bin/bash --

No luck

Last edited by nmuleski; 12-29-2011 at 12:24 PM.
 
Old 12-29-2011, 02:09 PM   #7
nmuleski
LQ Newbie
 
Registered: Mar 2011
Location: Madison, WI
Distribution: Ubuntu 10.10, CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: 0
Figured it out! I needed to put the full path of the mysql command!

So instead of :

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

I needed:

#!/bin/bash
/opt/bitnami/mysql/bin/mysql -uUSER -pPASSWORD db_name < /path/to/file.sql > /path/to/output.txt
 
Old 12-29-2011, 02:29 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Good for you.

You may want to consider adding the path to your PATH variable instead, however.

Code:
#!/bin/bash

export PATH="$PATH:/opt/bitnami/mysql/bin"

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

Also, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

Last edited by David the H.; 12-29-2011 at 02:31 PM. Reason: minor fix
 
Old 12-30-2011, 08:08 AM   #9
nmuleski
LQ Newbie
 
Registered: Mar 2011
Location: Madison, WI
Distribution: Ubuntu 10.10, CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: 0
Will putting that PATH line in my batch file save it for future use as well, or would I need to include it in any batch calling the mysql command?

Thanks for the formatting tip, I'll keep that in mind.
 
Old 12-30-2011, 09:52 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
"Batch File" is a windows term. They're called shell scripts here, or just "scripts".


The PATH is the environment setting that tells the shell where to look for the command names you run. If the directory containing the command you want to use isn't set in the default PATH, then you'll have to include it manually wherever you need it.

The directory above is obviously included in the PATH for your interactive shell, probably in bashrc or profile or a related startup file. But it's not stored in a file that is loaded for non-interactive scripts.

The INVOCATION section of your bash man page explains in detail which files and settings are used in your distribution when launching different types of shells. Read that and modify the appropriate settings if you want it to be globally available. Or just remember to manually set it each time if you do not.

Last edited by David the H.; 12-30-2011 at 09:56 AM. Reason: reworded for clarity
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running make menuconfig results in text file is busy. jerel4565 Linux - Kernel 2 07-18-2009 10:40 PM
Running command in gnome-terminal from script results in "...error creating child..." PlancksCnst Linux - General 6 04-08-2009 12:09 PM
Problems running batch file Poison Ivy 30 Linux - Newbie 3 01-16-2009 02:23 AM
Running a DOS batch file nyghtraven Linux - General 4 02-03-2007 02:18 AM
running ssh connection into batch file jdonaldogg General 1 12-17-2005 10:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration