简体   繁体   English

Bash和MySQL变量错误

[英]Bash and MySQL variables error

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: 我必须在``论坛'',``键''和``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

But if I add the back-ticks, bash interprets them as variables. 但是,如果我添加了反引号,bash会将它们解释为变量。

What should I do? 我该怎么办?

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'; 更新论坛集密钥='fnPIOid15anEJ2a3zVL6I1wbRjAKk0',其中id ='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. 单引号指示bash忽略内容,您将能够添加反引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM