1

I have the following script:

#!/bin/sh    
Q=`</dev/urandom tr -dc A-Za-z0-9 | head -c30`
mysql -uusername -ppasword accounts -e "update forum set key='$Q' where id='1';"

I have to add back-ticks (``) to "forum", "key" and "id", otherwise it returns me an error:

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 'key='xdindSG7hK9KaYgs9RISJNqrzmn4LJ' where id='1'' at line 1

But if I add the back-ticks, bash interprets them as variables.

What should I do?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
David Adders
  • 138
  • 3
  • 11

2 Answers2

2

Try a HERE document:

#!/bin/sh    
Q=`</dev/urandom tr -dc A-Za-z0-9 | head -c30`
mysql -uusername -ppasword accounts <<HERE
    update forum set key='$Q' where id='1';
HERE

Try with

cat <<HERE
    update forum set key='$Q' where id='1';
HERE

Output:

update forum set key='fnPIOid15anEJ2a3zVL6I1wbRjAKk0' where id='1';

sehe
  • 374,641
  • 47
  • 450
  • 633
1

Switch the single and double quotes. Single quotes instructs bash to ignore the contents and you will be able to add your back-ticks.

KQ.
  • 922
  • 4
  • 8
  • Sorry, missed that. Then just escape the backticks by preceeding them with a backslash. – KQ. Sep 27 '11 at 22:44