0

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
  • just a silly question here...is prefix a variable? or a string value? if a string value, shouldn't it be 'prefix'? Thanks. Commented Jan 9, 2023 at 20:59
  • it is string I tried that and I get this 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 Commented Jan 9, 2023 at 21:04

2 Answers 2

1

You are using a multi-line SQL statement. this means you have two options:

  1. you can push everything into one single line.
  2. try reformatting your script to use EOF tags instead.
  3. any option I didn't think of that the smart people here can consider...

here's an example:

mysql -u USER -pPASSWORD <<EOF
SQL_QUERY 1
SQL_QUERY 2
SQL_QUERY N
EOF

so for you, I would do this:

mysql -utesting -pMypass -hlocalhost -D test DB <<EOF
SELECT value FROM h6_options
where module=cloud
AND `option`='prefix';
EOF

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:

mysql -utesting -pMypass -hlocalhost -D test DB <<QUERY
SELECT `value` FROM `h6_options`
where `module`='cloud'
AND `option`='prefix';
QUERY

Source: https://www.shellhacks.com/mysql-run-query-bash-script-linux-command-line/

Sign up to request clarification or add additional context in comments.

13 Comments

that solved the first error, but now I get this: mysql: option '-e' requires an argument ./s3_clone.sh: line 3: SELECT: command not found ./s3_clone.sh: line 4: where: command not found ./s3_clone.sh: line 5: option: command not found ./s3_clone.sh: line 5: AND: command not found ./s3_clone.sh: line 6: QUERY: command not found
ditch the -e and see what that gives you...
Syntax to use near 'QUERY' at line 1 ./s3_clone.sh: line 3: SELECT: command not found ./s3_clone.sh: line 4: WHERE: command not found ./s3_clone.sh: line 5: option: command not found ./s3_clone.sh: line 5: AND: command not found ./s3_clone.sh: line 6: QUERY: command not found
my bad. only 2 <<'s are needed. sorry, I did it in my head first. :)...editing my answer.
ok. ditch the -e and only use 2 x << in the line. try that.
|
1

Turns out the issue was the backticks. I had to escape them in order to not evaluate the line.

<<QUERY
SELECT value FROM h6_options

WHERE \`option\`="prefix"

AND module="cloud"
QUERY

1 Comment

there you go. awesome....this was a good one.

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.