簡體   English   中英

Bash和MySQL變量錯誤

[英]Bash and MySQL variables error

我有以下腳本:

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

我必須在``論壇'',``鍵''和``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會將它們解釋為變量。

我該怎么辦?

試試這里的文檔:

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

輸出:

更新論壇集密鑰='fnPIOid15anEJ2a3zVL6I1wbRjAKk0',其中id ='1';

切換單引號和雙引號。 單引號指示bash忽略內容,您將能夠添加反引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM