LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running batch file does not produce same results as running command from Terminal (https://www.linuxquestions.org/questions/linux-newbie-8/running-batch-file-does-not-produce-same-results-as-running-command-from-terminal-920934/)

nmuleski 12-28-2011 02:12 PM

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!

bigrigdriver 12-28-2011 03:42 PM

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.

nmuleski 12-28-2011 04:41 PM

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.

Cedrik 12-28-2011 05:32 PM

There is something that you do differently in the bash script ?

Like use some variables ? If yes, did you define them correctly ?

jefro 12-28-2011 05:54 PM

Wonder if it is the dash or dash dash at #!/bin/bash


Try

#!/bin/bash --

nmuleski 12-29-2011 10:52 AM

Quote:

Originally Posted by Cedrik (Post 4560428)
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 (Post 4560445)
Wonder if it is the dash or dash dash at #!/bin/bash


Try

#!/bin/bash --


No luck :(

nmuleski 12-29-2011 02:09 PM

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

David the H. 12-29-2011 02:29 PM

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.

nmuleski 12-30-2011 08:08 AM

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.

David the H. 12-30-2011 09:52 AM

"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.


All times are GMT -5. The time now is 06:11 PM.