Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2011
    Location
    Madison, WI
    Posts
    11

    Unanswered: 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!

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    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
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    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.

  4. #4
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    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
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  5. #5
    Join Date
    Apr 2011
    Location
    Madison, WI
    Posts
    11
    Oh! Derp!

    That did it! Thanks so much for the help!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •