Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a temporary file in memory and using it as input file of a command

Tags:

bash

There is a command pdflatex, which I want to use in my bash script. It takes as input a filename on which content it will work.

Now I have an algorithms that looks as follows:

for stuff in list of stuff; do
  echo "${stuff}" > /tmp/tmpStuff
  pdflatex /tmp/tmpStuff
done 

Now this works as expected. But I was thinking I could speed that up by doing less disk I/O(as > redirection writes to a file). I wish I could write something like echo "$stuff" | pdflatex /tmp/tmpStuff but pdflatex uses a file and not stdin as its input. Is there any way of keeping "$stuff" in memory and passing it to pdflatex as a sort of file?

TLDR: I would be happy if I could create a temporary file which could be named and be in memory.

like image 940
Simonlbc Avatar asked Aug 30 '16 14:08

Simonlbc


2 Answers

You can use process substitution for this:

pdflatex <(echo "$stuff")

From the Bash Reference Manual:

3.5.6 Process Substitution

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of

<(list)

or

>(list)

The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list. Note that no space may appear between the < or > and the left parenthesis, otherwise the construct would be interpreted as a redirection.

And I also wonder if a here-string would make it as well:

pdflatex <<< "$stuff"
like image 152
fedorqui 'SO stop harming' Avatar answered Oct 01 '22 17:10

fedorqui 'SO stop harming'


Many shells, and Linux as a whole, accept:

echo "${stuff}"  | pdflatex /dev/stdin
like image 29
Gilbert Avatar answered Oct 01 '22 15:10

Gilbert