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! |
This is from the MySQL manual:
Quote:
Quote:
|
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. |
There is something that you do differently in the bash script ?
Like use some variables ? If yes, did you define them correctly ? |
Wonder if it is the dash or dash dash at #!/bin/bash
Try #!/bin/bash -- |
Quote:
---------- Post added 12-29-11 at 10:53 AM ---------- Quote:
No luck :( |
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 |
Good for you.
You may want to consider adding the path to your PATH variable instead, however. Code:
Also, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. |
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. |
"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 05:25 PM. |