bash 在内存中创建一个临时文件并将其用作命令的输入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39230353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating a temporary file in memory and using it as input file of a command
提问by Simonlbc
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.
有一个命令 pdflatex,我想在我的 bash 脚本中使用它。它需要一个文件名作为输入,它将处理哪些内容。
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?
现在这按预期工作。但我想我可以通过减少磁盘 I/O 来加快速度(因为 > 重定向写入文件)。我希望我可以写一些类似的东西,echo "$stuff" | pdflatex /tmp/tmpStuff
但是 pdflatex 使用一个文件而不是 stdin 作为它的输入。有什么方法可以将其保存"$stuff"
在内存中并将其作为一种文件传递给 pdflatex 吗?
TLDR: I would be happy if I could create a temporary file which could be named and be in memory.
TLDR:如果我能创建一个可以命名并在内存中的临时文件,我会很高兴。
回答by fedorqui 'SO stop harming'
You can use process substitution for this:
您可以为此使用流程替换:
pdflatex <(echo "$stuff")
From the Bash Reference Manual:
来自 Bash 参考手册:
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.
在支持命名管道 (FIFO) 或命名打开文件的 /dev/fd 方法的系统上支持进程替换。它采用以下形式
<(list)
或者
>(list)
进程列表运行时其输入或输出连接到 FIFO 或 /dev/fd 中的某个文件。该文件的名称作为扩展的结果作为参数传递给当前命令。如果使用
>(list)
表单,写入文件将为列表提供输入。如果使用<(list)
表单,则应读取作为参数传递的文件以获得列表的输出。请注意,< 或 > 和左括号之间不能出现空格,否则该构造将被解释为重定向。
And I also wonder if a here-string would make it as well:
而且我也想知道 here-string 是否也能做到:
pdflatex <<< "$stuff"
回答by Gilbert
Many shells, and Linux as a whole, accept:
许多 shell 以及整个 Linux 都接受:
echo "${stuff}" | pdflatex /dev/stdin
回答by Marinos An
Noneof the presented solutions is guaranteed to workon any command.
无所提出的解决方案是保证工作在任何命令。
Consider myCommand
below:
考虑myCommand
以下:
#!/bin/bash
# myCommand
test -f "" || { echo "error: Please provide a file"; exit 1; }
echo "The file content is:"$(cat )
Trying the existing solutions:
尝试现有的解决方案:
./myCommand <(echo "abcdef")
# error: Please provide a file
echo "abcdef" | ./myCommand /dev/stdin
# error: Please provide a file
In my Ubuntu I usethe following approach:
在我的 Ubuntu 中,我使用以下方法:
ramtmp="$(mktemp -p /dev/shm/)"
echo "abcdef" > $ramtmp
./myCommand $ramtmp
# The file content is:abcdef
So in your caseit would be:
所以在你的情况下,它将是:
ramtmp="$(mktemp -p /dev/shm/)"
for stuff in list of stuff; do
echo "${stuff}" > $ramtmp
pdflatex $ramtmp
done
In case pdflatex execution is non-blocking add the first line inside the loop.
如果 pdflatex 执行是非阻塞的,请在循环内添加第一行。
Note that, I'm not sure how many distributions currently support it. See Wikipedia article:
请注意,我不确定目前有多少发行版支持它。见维基百科文章:
Linux distributions based on the 2.6 kernel and later offer
/dev/shm
as shared memory in the form of a RAM disk, more specifically as a world-writable directory(a directory in which every user of the system can create files) that is stored in memory. Both the RedHatand Debian baseddistributions include itby default. Support for this type of RAM disk is completely optional within the kernel configuration file.[5]
基于2.6内核和后来提供Linux发行
/dev/shm
作为在RAM盘的形式共享存储器,更具体地作为世界可写的目录(其中系统的每一个用户可以创建文件的目录),其被存储在存储器中。无论是红帽和基于Debian的发行版包括它在默认情况下。对这种类型的 RAM 磁盘的支持在内核配置文件中是完全可选的。 [5]