Bash 和 MySQL 变量错误
我有以下脚本:
#!/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 会将它们解释为变量。
我应该怎么办?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试此处文档:
尝试
输出:
Try a HERE document:
Try with
Output:
交换单引号和双引号。单引号指示 bash 忽略内容,您将能够添加反引号。
Switch the single and double quotes. Single quotes instructs bash to ignore the contents and you will be able to add your back-ticks.