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?

Diana Eftaiha
  • 73
  • 2
  • 7
  • See [Unix: Passing Param to MYSQL files from BASH Shell Script](https://stackoverflow.com/questions/24603349/unix-passing-param-to-mysql-files-from-bash-shell-script). – wchiquito Oct 12 '22 at 13:11
  • Does this answer your question? [Unix: Passing Param to MYSQL files from BASH Shell Script](https://stackoverflow.com/questions/24603349/unix-passing-param-to-mysql-files-from-bash-shell-script) – Rohit Gupta Oct 12 '22 at 22:43
  • Neither work in my case – Diana Eftaiha Oct 14 '22 at 07:44

0 Answers0