up vote 0 down vote favorite
share [g+] share [fb]

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?

link|improve this question
3  
Use single quotes '' instead of "" around your SQL statement, or escape ` in it. Bash uses `` to execute commands inline. – XzKto Jul 6 '11 at 12:39
feedback

1 Answer

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\`";
link|improve this answer
1  
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. – Jonathan Leffler Jul 7 '11 at 0:57
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. – Emre Yazıcı Jul 7 '11 at 6:10
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. – Jonathan Leffler Jul 7 '11 at 6:17
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.