在内存中创建一个临时文件并将其用作命令的输入文件 [英] creating a temporary file in memory and using it as input file of a command

查看:55
本文介绍了在内存中创建一个临时文件并将其用作命令的输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个命令pdflatex,我想在我的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 

现在这可以按预期工作.但是我当时认为我可以通过减少磁盘I/O来加快速度(因为>重定向写入文件).我希望我可以写类似 echo"$ stuff" |pdflatex/tmp/tmpStuff ,但pdflatex使用文件而不是stdin作为输入.有什么方法可以将"$ stuff" 保留在内存中,并将其作为一种文件传递给pdflatex?

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:如果我可以创建一个可以命名并存在于内存中的临时文件,我会很高兴.

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

推荐答案

您可以为此使用进程替换:

You can use process substitution for this:

pdflatex <(echo "$stuff")

来自Bash参考手册:

From the Bash Reference Manual:

3.5.6流程替代

在支持命名管道的系统上支持进程替换(FIFO)或命名打开文件的/dev/fd方法.它采取的形式的

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)

>(list)

运行进程列表时,其输入或输出连接到FIFO或/dev/fd中的某个文件.作为扩展结果,此文件的名称作为参数传递给当前命令.如果使用>(list)形式,写入文件将为list提供输入.如果使用<(list)形式,则应读取作为参数传递的文件以获得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"

这篇关于在内存中创建一个临时文件并将其用作命令的输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆