简体   繁体   中英

pass parameter from bash to mysql script

I'm trying to pass parameter from bash script to mysql script. The bash script is

#!/bin/bash
for file in `ls *.symbol`
do
path=/home/qz/$file
script='/home/qz/sqls/load_eval.sql'
mysql -u qz -h compute-0-10 -pabc -e "set @pred = '$path'; source $script;" 
done

The load_eval.sql is

use biogrid;
load data local infile @pred into table lasp
fields terminated by ','
lines terminated by '\n'
(score, symbols);

When running the bash script, I got error the message:

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 '@pred into table lasp ..

It seems the value of the parameter @pred is not passed into mysql script.

MySQL doesn't support session variables in a LOAD DATA INFILE statement like that. This has been recognized as a feature request for quite some time ( http://bugs.mysql.com/bug.php?id=39115 ), but the feature has never been implemented.

I would recommend using mysqlimport instead of doing the complex steps with mysql that you're doing. The file's name must match the table's name, but you can trick this with a symbolic link:

#!/bin/bash
for file in *.symbol
do
  path="/home/qz/$file"
  ln -s -f "$path" /tmp/lasp.txt
  mysqlimport -u qz -h compute-0-10 -pabc \
    --local --columns "score,symbols" /tmp/lasp.txt 
done
rm -f /tmp/lasp.txt

PS: No need use `ls`. As you can see above, filename expansion works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM