2012-07-13 84 views
0

場景: 我正在嘗試一個小型的shell腳本程序。Shell腳本和sqlite3

在這個程序中 - 我試圖用2個表格查詢一個數據庫。並試圖獲得是或否的答案。

我使用python腳本從文件中獲取etag = md5sum

echo 'select a.hash,b.hashuser, case when a.hash=b.hashuser then "No!" else "yes!" end from tempo b, hashes a where a.hash=b.hashuser and b.hashuser='$etag'' 

當我嘗試打印我的屏幕上就說明它清楚地表明ETAG作爲的md5sum

但是,如果我嘗試查詢它在我的數據庫,並嘗試獲取結果。使用下面給出的腳本

sqlite3 hashez.db 'select a.hash,b.hashuser, case when a.hash=b.hashuser then "No!" else "yes!" end from tempo b, hashes a where a.hash=b.hashuser and b.hashuser='$etag'' 

這是我得到的錯誤。

Error: unrecognized token: "579f0b61cf958a0eea2f60906e6a04a4" 

google搜索一點點後,這個解決方案,我從這個link

發現然後我把它改成${#etag}

echo 'select a.hash,b.hashuser, case when a.hash=b.hashuser then "No!" else "yes!" end from tempo b, hashes a where a.hash=b.hashuser and b.hashuser='${#etag}'' 

,我得到的是現在

select a.hash,b.hashuser, case when a.hash=b.hashuser then "No!" else "yes!" end from tempo b, hashes a where a.hash=b.hashuser and **b.hashuser=32** 
錯誤

爲什麼是b.hashuser = 32。是我的第一個問題。

問題二:

當我嘗試使用上述功能來查詢數據庫:

sqlite3 hashez.db 'select a.hash,b.hashuser, case when a.hash=b.hashuser then "No!" else "yes!" end from tempo b, hashes a where a.hash=b.hashuser and b.hashuser='${#etag}'' 

我都沒有迴音了。

  • 是我的查詢錯誤。如果是,爲什麼我直接在數據庫上查詢時會得到答案?

對不起我的英文不好

回答

1

你消磨你的報價。

somecmd 'SELECT ... "'"$etag"'", ...' 

請注意單引號內部以及參數替換周圍的雙引號。

+0

感謝您的回覆。有用! 還有一個問題。 據我所知 - 「$ blah」 - >會將其作爲角色投擲。 但是'$'等於'會將它的值拋到變量中 我是否正確? – user1524529 2012-07-13 21:10:41

+0

不可以。單引號禁止參數替換,這就是爲什麼先關閉它們然後打開雙引號,然後再向後執行。 – 2012-07-13 21:20:10

+0

啊,我明白了!謝謝! – user1524529 2012-07-13 21:29:59