How to write a shell script for mysql?

Posted on 2009-02-22
Last Modified: 2012-05-06
I want to write a shell script for a unix-based system that will run a number of mysql commands:

mysql table < a.sql -u admin ...
mysql table < b.sql -u admin ...
mysql table < c.sql -u admin ...
mysql table < d.sql -u admin ...
mysql table < e.sql -u admin ...

I've really never done shell scripting before...  How would I go about doing this?
Question by:jen_jen_jen
    LVL 23

    Expert Comment

    Shouldn't it be "mysql database ..." instead of "mysql table ..."?

    Assuming you have your sql files listed in let's say sql.files.txt:
    for file in `cat /path/to/sql.files.txt`; do
       mysql database < ${file} -u admin ... 

    Open in new window


    Author Comment

    I tried that, but I get a bunch of ??? in the files and it says it can't read them?

    Author Comment

    I changed it to this:

    for file in $CURDIR;
       mysql database < ${file} -u admin ...  

    But, now I'm getting a sql error on a valid sql file:
    ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??' at line 1

    Do I need to remove the commented lines in the sql files or something?
    LVL 10

    Expert Comment

    >>Do I need to remove the commented lines in the sql files or something?
    Normally not, can you post the the SQL with the problematic lines
    LVL 23

    Accepted Solution

    Sorry - I rewrote your command without looking at it exactly.
    As far as I know, options have to be placed right after "mysql" - before database name. So command (in for loop) should be:
    mysql -u admin -rest_of_your_options_here database < ${file}

    If this doesn't work, paste here error messages you are getting.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now