1

我有以下脚本:

#!/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';"

我必须在“forum”、“key”和“id”中添加反引号 (``),否则会返回错误:

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

但如果我添加反引号,bash 会将它们解释为变量。

我该怎么办?

4

2 回答 2

2

试试 HERE 文档:

#!/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

尝试

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

输出:

更新论坛设置 key='fnPIOid15anEJ2a3zVL6I1wbRjAKk0' where id='1';

于 2011-09-27T22:46:29.247 回答
1

切换单引号和双引号。单引号指示 bash 忽略内容,您将能够添加反引号。

于 2011-09-27T22:33:42.457 回答