[Tex/LaTex] how to execute external commands from (Xe)(La)TeX

external filesshell-escapexetex

i'm trying to capture output from an external command in a XeLaTeX document. as per https://tex.stackexchange.com/a/20566/28067 and https://stackoverflow.com/a/8929976/256361, i'm specifically doing

\documentclass[a5paper]{article}
\usepackage{polyglossia}
\setdefaultlanguage{english}

\begin{document}

A A A

\immediate\input{|"node -v"}

B B B

\end{document}

i'm running TeX with xelatex --enable-write18 --enable-pipes write18.tex to enable features normally blocked (because security).

however, this does not appear to work. i've tried the \input command with and without the braces, and with and without \immediate, but as long as that line is in the source, XeLaTeX concludes output with

(./write18.aux
)

rather than

(./write18.aux) )
Output written on write18.pdf (1 page).
Transcript written on write18.log.

(ie there is an additional newline, and output just stops dead without the remaining lines). observe that on my system, the output of node -v is the completely un-dangerous string v0.11.7. i understand that in order to capture more arbitrary output i'd have to fiddle with catcodes, but that's food for another question.

so what am i doing wrong here?

Update @egreg you might be right! i tried

\immediate\write18{node -v > /tmp/temp.dat}
\input{/tmp/temp.dat}

and that did work. funny though it would appear you told readers in a comment to https://tex.stackexchange.com/a/20566/28067 that "in TeX Live 2013 the syntax \input|"command" is supported also by XeTeX"–were you in error here?

btw the fact that (in TeXLive) using non-supported command line options do not seem to trigger error messages does not exactly help make testing easier. also funny that http://docs.miktex.org/manual/xetex.html has all the long options prefixed with two dashes where xelatex --help gives them with a single dash (and fails to even mention that many more options are available).

Best Answer

I used this

\documentclass{article}

\begin{document}

\makeatletter
\@@input|"dir"

\end{document}

which gave me some errors as the output of dir also contains a path with a backslash but beside this did what was expected: I got 12 pages of more or less sensible output (my test folder is large ;-)). I called xelatex with --enable-pipes --shell-excape. It worked with miktex and TL 2013 on windows. (--enable-pipes is not known in texlive but did no harm).

Related Question