开发者

SQLite, bash script, and concatenate operator

I have a SQLite database, stuff.db (with TABLE notes (id INTEGER PRIMARY KEY, timeStamp DATE, note TEXT);). The timeStamp is automatically triggered.

I wa开发者_运维知识库nt to populate it the quick and dirty way from the command line.

So, I have written a script named bashscript1 which goes :

sqlite3 script.db 'insert into notes (note) values ("Stuff happens.");'

But I wanted both to keep this sentence as the standard entry and if necessary, to be more precise : I would like to add variable information such as : "Foo enters the room." or "Bar makes a phone call.". The idea is to concatenate those strings and have : "Stuff happens. Foo enters the room." or "Stuff happens. Bar makes a phone call."

So I rewrote the script :

echo "Do you want to add somme text ?"
read Str1
sqlite3 script.db 'insert into notes (note) values ("Stuff happens."||\"$Str1\");'

And, of course I got an error message. How should I proceed ?

Thanks in advance

ThG


Try:

sqlite3 script.db "insert into notes (note) values ('Stuff happens.$Str1');"

Note that this will break if your $Str1 contains a single quote.


Shell variables wont expand when wrapped in single-quotes like you have it. Perhaps you want:

sqlite3 script.db "insert into notes (note) values (\"Stuff happens.\"||\"$Str1\");"

P.S. I'm assuming the || is part of the sqlite3 syntax because it certainly isn't bash, at least not in that context. If that's not right, let me know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜