I was very happy when I read egreg's 2019 Addendum about how to launch a shell script from LaTeX and grab the output.
But when I tried to use it to connect with a SQLite database, I got a LaTeX3 error. Here is the (minimal) code:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\captureshell}{som}
{
\sdaau_captureshell:Ne \l__sdaau_captureshell_out_tl { #3 }
\IfBooleanT { #1 }
{% we may need to stringify the result
\tl_set:Nx \l__sdaau_captureshell_out_tl
{ \tl_to_str:N \l__sdaau_captureshell_out_tl }
}
\IfNoValueTF { #2 }
{
\tl_use:N \l__sdaau_captureshell_out_tl
}
{
\tl_set_eq:NN #2 \l__sdaau_captureshell_out_tl
}
}
\tl_new:N \l__sdaau_captureshell_out_tl
\cs_new_protected:Nn \sdaau_captureshell:Nn
{
\sys_get_shell:nnN { #2 } { } #1
\tl_trim_spaces:N #1 % remove leading and trailing spaces
}
\cs_generate_variant:Nn \sdaau_captureshell:Nn { Ne }
\ExplSyntaxOff
\begin{document}
\captureshell{sqlite3 /hom/yannis/texmf/yannis.db 'select title from title where id="grafematik2020/hara/hara.tex"'}
\end{document}
The error says I'm not allowed to use double-quotes in shell commands:
! LaTeX3 Error: Quotes in shell command 'sqlite3 /hom/yannis/texmf/yannis.db
(LaTeX3) 'select title from title where
(LaTeX3) id="grafematik2020/hara/hara.tex"''.
For immediate help type H <return>.
...
l.34 ... where id="grafematik2020/hara/hara.tex"'}
? H
Shell commands cannot contain quotes (").
How can I nest a string inside an SQL command inside a shell command line, without using double-quotes?
Writing the value of the id argument without quotes is against SQL syntax: it considers it as a column name…
[PS. No, I do not wish to use LuaTeX or Context, even though I know that they are better suited for talking with sqlite3.]
\inputdrops quotes when scanning a file name (even if you use the syntax with braces available since last year), so when you do the command\input{|ls "/some/path/with spaces"}it will execute the shell commandls /some/path/with spaces.expl3just warns you that your command won't work as intended due to the engine limitation. I'm afraid you'll have to find your way around without the quotes – Phelype Oleinik Jan 18 '21 at 14:00\input{...}, but that would be inconsistent with other primitives like\pdffilesize, that take a braced file name and also drops quotes. Basically we're stuck with no quotes in\input:( – Phelype Oleinik Jan 18 '21 at 14:03\'to get a single quote in to a '-delimted string at the shell level,but a generic way to avoid all quoting issues is to use\immediate\writeto write the entire command line totmp.shthen use write 18 to executebash ./tmp.sh– David Carlisle Jan 18 '21 at 14:10"(ASCII 34). Knuth's original implementation stopped scanning a file name at a space character, so the quote syntax was introduced to allow\input "/some/file/with spaces", and the implementation recklessly kills all"in the string. – Phelype Oleinik Jan 18 '21 at 14:11