6

I want to execute a .sql file in a bash file but I need to pass parameters to the .sql from the bash file. I think it's a simple solution but I can't figure it out.

This is what I have so far:

.SQL File

SET @columnValue = &1;
UPDATE tblTest SET Description = @columnValue;

Bash File

#!/bin/bash
columnValue=$1
mysql -uroot -ppassword testDB < SQLscript.sql $columnValue

Shell

sh myBashFile.sh "testColumnValue"

Can anyone tell me what i am doing wrong?

1
  • can you tell us what is currently happening? Commented Oct 15, 2014 at 8:39

1 Answer 1

1

You need a combination of setting SQL parameters first and then loading your .sql file.

In your bash script you can use mysql with the -e option to execute an SQL statement. The same method must then be used to tell mysql to load the file instead of reading it using bash.

See this example script.

If you only want to execute a single statement, see this question.

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

2 Comments

Your solution is working fine thank you!! :) So if i understand it correctly there is no way to pass params to a .sql file without setting mysql variable ?
You could generate a bash script that replaces the variable tokens in your .sql file before handing it to mysql. However, I guess that would unnecessarily complicate things.

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.