Run .sql file in bash with options

I want to execute a .sql file in a bash file, but I need to pass parameters to .sql from a bash file. I think this is a simple solution, but I cannot understand.

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 someone tell me what I am doing wrong?

+6
source share
1 answer

First you need to set the SQL parameters first and then load the .sql file.

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

See this example script .

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

+1
source

Source: https://habr.com/ru/post/976766/


All Articles