1

I'm trying to simplify a load data local infile, by putting it into a .sh file, and bash to run it.

Here is my count_portal.sh file

mysql -h host -u root -p password 
load data local infile
"/workplace/user/dump/count_portal.txt" 
replace into table test.icqa_count_portal 
fields terminated by '\t' lines terminated by '\n' ignore 1 lines;

And here is my bash script

bash /home/user/Desktop/count_portal.sh I get an output that doesn't do what it is designed to do. When I simply make the count_portal.sh contain mysql -h host it longs in when running the script.

2
  • 3
    so what's the questions? Commented Dec 5, 2013 at 21:20
  • 1
    How I would go about making this .sh work. Currently it doesn't read anything after mysql-h host -u root -p password. It appears load data local infile isn't recognized or not working. Although it does work independently while connected to mysql. Commented Dec 5, 2013 at 21:21

5 Answers 5

1

I figured it out. Here is my file.

#!/bin/bash
/usr/bin/mysql --host=host --user=root --password=password --database=test<<EOFMYSQL
load data local infile '/workplace/user/ETLdump/count_portal.txt' replace INTO TABLE count_portal fields terminated by '\t' LINES
TERMINATED BY '\n' ignore 1 lines;
EOFMYSQL

Works flawlessly!

Sign up to request clarification or add additional context in comments.

Comments

0

Try removing the space between the user and password switches. So it would be something like:

mysql -h host -uroot -ppassword etc....

The spaces seem to cause problems for me when doing similar things inside bash scripts.

Comments

0

missed -e ?

mysql -h host -u root -ppassword dbname -e "load data local infile '/workplace/user/dump/count_portal.txt' replace into table test.icqa_count_portal fields terminated by '\t' lines terminated by '\n' ignore 1 lines;"

3 Comments

Tried this same result, just gave me a bunch of options of example commands.
@Spartacus38 Emm, two more things, should no space between -p and pasword, and you missed db name.
That did, something, it generates a new line. dquote >
0

I agree with @PasteBT, his answer should work well. I think there are escape problem or shell variable is empty.

Have you tried this?:

echo "LOAD DATA LOCAL INFILE '/workplace/user/dump/count_portal.txt' REPLACE INTO TABLE test.icqa_count_portal FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' IGNORE 1 LINES;" | mysql -h host -u root -ppassword

and you original shell script is wrong. could you post again what did you do?

Comments

0
mysql -u<username> -p<password> -h<hostname> <db_name> --local_infile=1 -e "use <db_name>" -e"LOAD DATA LOCAL INFILE '<path/file_name>' 
IGNORE INTO TABLE <table_name>
FIELDS TERMINATED BY '\t'
OPTIONALLY ENCLOSED BY '\"'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.