LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-04-2012, 09:03 AM   #1
kha.t.nguyen
LQ Newbie
 
Registered: Feb 2011
Posts: 9

Rep: Reputation: 0
Bash: error using a for loop with mysql -e source


Hi everyone,
I have a mysqldump that was a relatively large file(8gb) and I split it into 238 manageable files. I created a bash script to loop through each of the files, but am getting an error. What am I doing wrong on the /usr/bin/mysql -e 'source... ' line?

Thanks all.


Here's the script:


#! /bin/bash
i=0
for ((i=0;i<239;i++))
do
echo "Processing $i.sql"
/usr/bin/mysql -e 'source $i.sql'
done



And I'm receiving the following error for each iteration:

Processing 233.sql
ERROR at line 1: Failed to open file '$i.sql', error: 2
Processing 234.sql
ERROR at line 1: Failed to open file '$i.sql', error: 2
Processing 235.sql
ERROR at line 1: Failed to open file '$i.sql', error: 2
Processing 236.sql
ERROR at line 1: Failed to open file '$i.sql', error: 2
Processing 237.sql
ERROR at line 1: Failed to open file '$i.sql', error: 2
Processing 238.sql
 
Old 02-04-2012, 11:38 AM   #2
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Rep: Reputation: 39
Quote:
/usr/bin/mysql -e 'source $i.sql'
Did you try to run this on any individual sql file and see whether it gives the same error or not? It seems like it can't read the sql files so did u check the file permissions also just in case.
 
Old 02-04-2012, 11:53 AM   #3
kha.t.nguyen
LQ Newbie
 
Registered: Feb 2011
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tanveer View Post
Did you try to run this on any individual sql file and see whether it gives the same error or not? It seems like it can't read the sql files so did u check the file permissions also just in case.
Thanks for the quick response. Permissions are ok(all owned by root). inside mysql I can run the first file, and mysql -e doesn't error:

mysql> source 0.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

[root@ao_bn individual_sql_files]# mysql -e 'source 0.sql'
[root@ao_bn individual_sql_files]# /usr/bin/mysql -e 'source 0.sql'
[root@ao_bn individual_sql_files]#

Last edited by kha.t.nguyen; 02-04-2012 at 11:55 AM.
 
Old 02-04-2012, 12:00 PM   #4
kha.t.nguyen
LQ Newbie
 
Registered: Feb 2011
Posts: 9

Original Poster
Rep: Reputation: 0
Following up to my post. I found a solution:

/usr/bin/mysql -e 'source $i.sql'


Should have double quotes:
/usr/bin/mysql -e "source $i.sql"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash to loop thorouh mysql select array z01krh Programming 3 12-27-2017 08:30 PM
bash and simple loop: error frenchn00b Programming 2 01-24-2010 05:48 PM
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration