Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute .sql file in bash with params

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?

like image 261
Moenie Avatar asked Oct 15 '14 08:10

Moenie


1 Answers

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.

like image 90
andy Avatar answered Oct 27 '22 00:10

andy