bash tempfile 和 mktemp 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18716658/
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
What's the difference between tempfile and mktemp?
提问by Thomas
Most systems I've encountered have both tempfile(1)
and mktemp(1)
. There are syntactic differences, and mktemp
can also create directories, but they otherwise seem to do the same thing.
我遇到的大多数系统都有tempfile(1)
和mktemp(1)
。存在语法差异,也mktemp
可以创建目录,但它们似乎做同样的事情。
Is there any difference between the two? Why do both exist? Is one more standard than the other? If I just want to create a temporary file safely, is there any reason to prefer one over the other?
两者之间有什么区别吗?为什么两者都存在?一个比另一个更标准吗?如果我只想安全地创建一个临时文件,是否有任何理由更喜欢一个?
I suspect there's some interesting Unix lore behind this, but my searches turn up nothing.
我怀疑这背后有一些有趣的 Unix 知识,但我的搜索没有结果。
回答by devnull
I suspect there's some interesting Unix lore behind this ...
我怀疑这背后有一些有趣的 Unix 知识......
The historyof mktemp
can be traced to OpenBSD 2.1. However, it became a part of GNU coreutils much later. Thispost announced the inclusion of mktemp
for coreutils
.
在历史的mktemp
可以追溯到OpenBSD的2.1。然而,它后来成为 GNU coreutils 的一部分。 这篇文章宣布包含mktemp
for coreutils
。
Until then, tempfile
was being used by a number of programs. There was also a proposalto make tempfile
a wrapper around mktemp
, which was rejectedto discourage use of tempfile
.
在此之前,tempfile
已被许多程序使用。还有一个建议是制作tempfile
一个包装器mktemp
,但被拒绝以阻止使用tempfile
.
However, the following was added to tempfile
manual:
但是,以下内容已添加到tempfile
手册中:
Exclusive creation is not guaranteed when creating files on NFS partitions.
tempfile
cannot make temporary directories.tempfile
is deprecated; you should usemktemp(1)
instead.
在 NFS 分区上创建文件时,不保证独占创建。
tempfile
无法创建临时目录。tempfile
已弃用;你应该mktemp(1)
改用。
回答by konsolebox
It says in tempfile's manual that:
它在 tempfile 的手册中说:
tempfile - create a temporary file in a safe manner
tempfile - create a temporary file in a safe manner
While in mktemp:
在 mktemp 中:
mktemp - create a temporary file or directory
mktemp - create a temporary file or directory
They're probably just almost the same, only that the implementation is a little different.
它们可能几乎相同,只是实现方式略有不同。
As said in the manual, tempfile
actually has some precautions like:
正如手册中所说,tempfile
实际上有一些预防措施,例如:
a) In case the environment variable
TMPDIR
exists and contains the name of an appropriate directory, that is used.b) Otherwise, if the
--directory
argument is specified and appropriate, it is used.c) Otherwise,
P_tmpdir
(as defined in<stdio.h>
) is used when appropriate.d) Finally an implementation-defined directory (
/tmp
) may be used.
a) 如果环境变量
TMPDIR
存在并且包含适当目录的名称,则使用该名称。b) 否则,如果
--directory
参数已指定且适当,则使用它。c) 否则,在适当的时候使用
P_tmpdir
(如 中定义的<stdio.h>
)。d) 最后可以使用实现定义的目录 (
/tmp
)。
It's actually useful if the script trusts mktemp
or tempfile
enough that it's certain that a temporary file or directory would be created. But I don't see much problem just using mktemp
if you run precautions in your script yourself. You can use [ -e ]
, [ -f ]
, [ -d ]
, [ -L ]
, etc. to verify if a file could actually be made/was already made. Even check if something's writable, readable, and/or executable with -r, -w and -x. In bash, see help test
.
如果脚本信任mktemp
或tempfile
足够确定会创建临时文件或目录,它实际上很有用。但是,mktemp
如果您自己在脚本中运行预防措施,我认为仅使用不会有太大问题。您可以使用[ -e ]
、[ -f ]
、[ -d ]
、[ -L ]
等来验证文件是否可以实际制作/已经制作。甚至可以使用 -r、-w 和 -x 检查某些内容是否可写、可读和/或可执行。在 bash 中,请参阅help test
.
Still for the sake of continuous runtime perhaps you would better rely on tempfile
when running your code in multiple environments. Just make sure that it's available everywhere enough. With which
or with type -P
you could check which one of them is available. An example:
仍然为了连续运行tempfile
时,在多个环境中运行代码时,您可能会更好地依赖。只要确保它在任何地方都足够可用。使用which
或使用type -P
您可以检查其中哪一个可用。一个例子:
create_temp() {
if type -P tempfile >/dev/null; then
# use tempfile based from
elif type -P mktemp > /dev/null; then
# use mktemp based from
else
echo "Can't find temporary file creator."
exit 1
fi
}
回答by hek2mgl
From the source code sight of things (on debian) tempfile
is from the package debianutils
and uses the libc function tempnam()
while mktemp
is from GNU coreutils and doesn't use the libc function
从源代码来看(在 debian 上)tempfile
来自包debianutils
并使用 libc 函数,tempnam()
而mktemp
来自 GNU coreutils 并且不使用 libc 函数