简体   繁体   English

将插值的sqlite3查询分配给Bash中的变量的正确引用是什么?

[英]What is the proper quoting to assign an interpolated sqlite3 query to a variable in Bash?

I am trying to assign the output of the following to a variable 我试图将以下输出分配给变量

sqlite3 /home/user/db_fake_book_index "select id, page from fb2 where bookcode='$bookcode' and page=$page

If I run quote it like 如果我运行报价就好

echo "sqlite3 /home/user/db_fake_book_index \"select id, page from fb2 where bookcode='$bookcode' and page=$page\""

I get 我明白了

sqlite3 /home/user/db_fake_book_index "select id, page from fb2 where bookcode='557S' and page=10"

and if I copy it to a console it works fine, but if i encase it in $() in my script it does not work, sqlite believes there are too many variables, it seems my double quotes are being removed. 如果我将它复制到控制台它工作正常,但如果我把它包装在我的脚本中的$()它不起作用,sqlite认为有太多的变量,似乎我的双引号被删除。

$(printf "sqlite3 /home/user/db_fake_book_index \"select id, page from fb2 where bookcode='$bookcode' and page=$page\"")

This gives the same faliure 这给出了相同的效果

result=$( sqlite3 /home/user/db_fake_book_index "select id, page from fb2 where bookcode='$bookcode' and page=$page" )

or 要么

query="select id, page from fb2 where bookcode='$bookcode' and page=$page"
result=$( sqlite3 /home/user/db_fake_book_index "$query" )

This will save as the variable v 这将保存为变量v

set "select id, page from fb2 where bookcode='$bookcode' and page=$page"
read v < <(sqlite3 /home/user/db_fake_book_index "$1")

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

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