0

I am trying to run MySQL query through bash script. But, when I run SELECT * FROM EXAMPLE_DB; inside bash scripting, it is translated to SELECT files1 files2 files3 where I run the script.

Example :

read -d '' SQL_QUERY << EOF
SET @var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT * FROM tassta.vontista_messages WHERE date(sent_date) >= date(@var_name);
EOF
echo ${SQL_QUERY} | mysql

What I want to run the mysql query as it is. What happened now that this is translated to

read -d '' SQL_QUERY << EOF
SET @var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT file1 file2 file3 [files from where I run the script.] FROM tassta.vontista_messages WHERE date(sent_date) >= date(@var_name);
EOF
echo ${SQL_QUERY} | mysql
Mhd Ridho Swasta
  • 105
  • 1
  • 12

1 Answers1

0
SQL_QUERY="SET @var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT * FROM tassta.vontista_messages WHERE date(sent_date) >= date(@var_name);"

mysql -Be "$SQL_QUERY"

or:

echo "$SQL_QUERY" | mysql 

NOTE: Do not put spaces before, or after the =.

see: How do I escape the wildcard/asterisk character in bash?

Luuk
  • 9,042
  • 4
  • 20
  • 28