0

Running a bash script executing MySQL commands, I get an error on this line.

$MYSQL_BIN $DATABASE -e \
"ALTER TABLE `nodes` ADD COLUMN `created_date` int(32) AFTER `address`";

The error is created_date: command not found

As well as on this line:

$MYSQL_BIN $DATABASE -e \
"UPDATE `nodes` SET `created_date` = UNIX_TIMESTAMP() WHERE `created_date` 
IS NULL AND `address` IS NOT NULL";

ERROR 1064 (42000) at line 1: 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 'SET = UNIX_TIMESTAMP() WHERE IS NULL AND IS NOT NULL' at line 1.

I believe the first error is because I'm probably not escaping quotes?

1
  • 3
    Use single quotes '' instead of "" around your SQL statement, or escape ` in it. Bash uses `` to execute commands inline. Commented Jul 6, 2011 at 12:39

1 Answer 1

4

Bash uses bactick operator (`) to indicate command substitution, that is, substitution of the standard output from one command into a line of text defining another command.

So you should either use single quotes instead of double ones or escape the backticks properly:

$MYSQL_BIN $DATABASE -e 
'ALTER TABLE `nodes` ADD COLUMN `created_date` int(32) AFTER `address`';

or

$MYSQL_BIN $DATABASE -e 
"ALTER TABLE \`nodes\` ADD COLUMN \`created_date\` int(32) AFTER \`address\`";
Sign up to request clarification or add additional context in comments.

3 Comments

One is left wondering what the nodes command generated as output. But that's not a problem with your answer - just with the OP's environment.
There is no command named nodes on a standart *NIX (at least Linux) installation, so the output is most probably -bash: nodes: command not found.
I wonder if we got all the error messages; there was also the command address too. Nice to know there isn't a standard command by the name 'nodes' - thanks.

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.