Reputation: 641
I'm trying to load some csv files by calling mysql from the terminal without entering mysql interpreter.
I created the following function which I call when I'm ready to load all csv files mentioned in "$@"
function sqlConn {
sqlLoad="$sqlConnBase $@ $dbName"
`"$sqlLoad"`
#I tried simply with $sqlLoad too but same problem occurs,
#although everything needed for the query is present in either
#$sqlLoad or "$sqlLoad"
}
sqlConnBase
and dbName
are global variables defined at the beginning of my bash script like this:
sqlConnBase="mysql -h localhost -u group8 --password=toto123"
dbName="cs322"
I call sqlConn
like this:
sqlConn " --local-infile=1 < sqlLoadFile.sql"
the content of sqlLoadFile.sql
is the following:
LOAD DATA LOCAL INFILE 'CSV/notes_rem.csv'
INTO TABLE Notes
CHARACTER SET UTF8
FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY '';
The problem I get is the following:
./loadAll.bash: line 31: mysql -h localhost -u group8
--password=toto123 --local-infile=1 < sqlLoadFile.sql cs322: command not found
the strange thing is that when I simply execute
mysql -h localhost -u group8 --password=toto123
--local-infile=1 < sqlLoadFile.sql cs322
on my terminal it does populate my cs322 database, i.e. all the rows of my csv are present in my cs322 database.
What could be the source of the error in my script?
Upvotes: 0
Views: 37
Reputation: 47099
The mysql -h localhost ...
is treated as a command and not just mysql
where the rest is arguments.
You need to use eval
instead of the backticks:
eval "$sqlLoad"
When that is said you should be really careful with escapes, word splitting and globbing, and the above approach should be avoided.
A recommended approach is to populate an array with arguments:
declare -a args
args+=("-h" "localhost")
args+=("-u" "group")
# ...
mysql "${args[@]}"
Upvotes: 1