1

I am trying to pass parameters from a shell script to an sql file, to be used in the MySQL queries contained in said sql file. What I am currently able to do is set the variable at the beginning of the sql file like so:

SET @name := 'Helen';
insert into testMe values (@name);

And then execute the sql file from my shell script like so:

#!/bin/bash

SQL_FILE="test.sql"
DB_HOST=$1
MYSQL_CONTAINER=$2

docker exec -i $MYSQL_CONTAINER sh -c "mysql -h $DB_HOST -u user -ppassword -D dbname" < "$SQL_FILE"

This will successfully insert a new row with Helen as the value.

mysql> select * from testMe;
+-------+
| name  |
+-------+
| Helen |
+-------+

What I would like to do instead, though, is pass the variable @name from the shell script file itself, rather than define it at the top of the sql file. Can anyone tell me how I can achieve that?

3

0

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.