Bobby
Bobby

Reputation: 320

Passing multiple parameters from shell script to mysql query

I have to write a unix shell script which will take the parameters for MySQL and export the result into csv file. I have written to some extent but am unable to pass the multiple parameters from shell script to sql.

Can anyone help me out in this? Thanks!!

Upvotes: 1

Views: 2103

Answers (2)

a14m
a14m

Reputation: 8055

assuming you call the script like this $ ./script param1 param2 param3

in the script

echo $0 #will echo 'script' (the name of the script)
echo $1 #will echo 'param1'
echo $2 #will echo 'param2'
echo $3 #will echo 'param3'
echo $# #will echo '3' the number of params passed to script
echo $@ #will echo 'param1 param2 param3' (all the parameters passed)
host="127.0.0.1"
user="root"
password="pass"
result=`mysql -h $host --user=$user --password=$password --skip-column-names -e "select $param1 from $param2 where $param3 = 3"`
echo $result

Upvotes: 3

newtover
newtover

Reputation: 32094

mysql -e "set @param1:=4897, @param2:=2; source the_script.sql;"

And the script:

SELECT @param1, @param2;

P.S. BTW, I recommend to pass connection parameters using --defaults-extra-file option.

Upvotes: 2

Related Questions