I am trying to execute a mysql command inside a bash script but every time I try to execute it fails for some reason. I have tried several ways and none seemed to work(e.g: <<QUERY…;QUERY)
My select is the following but I get an error:
#!/bin/bash
mysql -utesting -pMypass -hlocalhost -D test DB -e "
SELECT value FROM h6_options
where module=cloud
AND `option`=prefix;"
I get the following error.
ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='prefix'' at line 5
Any help is appreciated.
2
Answers
Turns out the issue was the backticks. I had to escape them in order to not evaluate the line.
You are using a multi-line SQL statement. this means you have two options:
EOF
tags instead.here’s an example:
so for you, I would do this:
Note: I don’t like using the word EOF….for me, I like the word SQL or QUERY. because EOF means end of file. The code is identical, so you can use the word of choice:
Source: https://www.shellhacks.com/mysql-run-query-bash-script-linux-command-line/