TeX has support for file IO, and you can take advantage of this. To create a new input filehandle, you execute \newread\readFH
; at this point, \readFH
is a number representing a channel on which you can read or write (you've already seen one of these, the special channel 18
). To open the file, you run \immediate\openin\readFH=filename.ext
; now, reading from channel \readFH
will read lines from filename.ext
. To actually read from the file, you run \immediate\read\readFH to \nextline
; this reads one line from \readFH
and puts it in \nextline
. Closing the file is then done with \immediate\closein\readFH
.
Note that this treats newlines as spaces; if you have a file containing e.g.
foo
bar
and read a line into \nextline
, it will be as though you wrote \def\nextline{foo }
. To avoid this, you set \endlinechar
to -1
.
Overall, then, your example would look like this:
\newread\myinput
% We use '\jobname.temp' to create a uniquely-named temporary file
\immediate\write18{some command > '\jobname.temp'}
\immediate\openin\myinput=\jobname.temp
% The group localizes the change to \endlinechar
\bgroup
\endlinechar=-1
\immediate\read\myinput to \localline
% Since everything in the group is local, we have to explicitly make the
% assignment global
\global\let\myresult\localline
\egroup
\immediate\closein\myinput
% Clean up after ourselves
\immediate\write18{rm -f -- '\jobname.temp'}
\dosomething{\myresult}
You could probably abstract this into a macro, but precisely what parts ought to be parametrized probably depends on your specific use case.
Also, just for future reference: creating filehandles to write to is done with \newwrite
, you open them with \immediate\openout\writeFH=filename.ext
, you write to them with \immediate\write\writeFH{some text}
, and you close them with \immediate\closeout\writeFH
.