1

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!!

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

2
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.

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.