Link to home
Start Free TrialLog in
Avatar of jen_jen_jen
jen_jen_jen

asked on

How to write a shell script for mysql?

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?
Avatar of Maciej S
Maciej S
Flag of Poland image

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

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

Open in new window

Avatar of jen_jen_jen
jen_jen_jen

ASKER

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

#!/bin/sh
CURDIR="/tmp/"
for file in $CURDIR;
do
   mysql database < ${file} -u admin ...  
done


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?
>>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
ASKER CERTIFIED SOLUTION
Avatar of Maciej S
Maciej S
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial