如何在 bash 中创建具有特定扩展名的临时文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2419754/
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
How can I create a temp file with a specific extension in bash?
提问by cobbal
I'm writing a shell script, and I need to create a temporary file with a certain extension.
我正在编写一个 shell 脚本,我需要创建一个具有特定扩展名的临时文件。
I've tried
我试过了
tempname=`basename tempname=`basename tempname=`basename --suffix=SUFF
append SUFF to TEMPLATE. SUFF must not contain slash. This option is implied if TEMPLATE does not end in X.
$ mktemp /tmp/banana.XXXXXXXXXXXXXXXXXXXXXXX.mp3
/tmp/banana.gZHvMJfDHc2CTilINNuq2P0.mp3
`
TMPTMP=`mktemp -t ${tempname}` || exit 1
TMPPS="$TMPTMP.$$.ps"
mv $TMPTMP $TMPPS || exit 1
`
TMPPS=`mktemp -t ${tempname}` || exit 1
`
TMPPS=`mktemp /tmp/${tempname}.XXXXXX.ps` || exit 1
and
和
echo $(mktemp $TMPDIR/$(uuidgen).txt)
neither work, as the first creates a file name with a literal "XXXXXX" and the second doesn't give an option for an extension.
都不起作用,因为第一个创建了一个带有文字“XXXXXX”的文件名,第二个没有提供扩展名的选项。
I need the extension so that preview won't refuse to open the file.
我需要扩展名,以便预览不会拒绝打开文件。
Edit:I ended up going with a combination of pid and mktemp in what I hope is secure:
编辑:我最终选择了 pid 和 mktemp 的组合,我希望是安全的:
mydir=`mktemp -d`
touch "$mydir"/myfile.ps
It is vulnerable to a denial of service attack, but I don't think anything more severe.
它很容易受到拒绝服务攻击,但我认为没有比这更严重的了。
回答by basic6
Recent versions of mktemp offer --suffix:
最新版本的 mktemp 提供 --suffix:
tempname=`basename temp=$(mktemp -u).ps
: >"$temp"
`
TMPPS_PREFIX=$(mktemp "${TMPDIR:-/tmp/}${tempname}.XXXXXX")
TMPPS=$(mktemp "${TMPPS_PREFIX}.ps")
rm ${TMPPS_PREFIX}
echo "Your temp file: ${TMPPS}"
I believe this requires coreutils >= 8 or so.
我相信这需要 coreutils >= 8 左右。
If you create a temp file (older mktemp version) without suffix and you're renaming that to append one, the least thing you could probably do is check if the file already exists. It doesn't protect you from race conditions, but it does protect you if there's already such a file which has been there for a while.
如果您创建了一个没有后缀的临时文件(旧的 mktemp 版本),并且您将其重命名为附加一个,那么您可能要做的最少的事情就是检查该文件是否已经存在。它不会保护您免受竞争条件的影响,但如果已经有这样的文件存在一段时间,它确实会保护您。
回答by Stephan
How about this one:
这个怎么样:
##代码##回答by Lucas Gonze
All of these solutions except --suffix (which isn't always available) have a race condition. You can eliminate the race condition by using mktemp -d to create a directory, then putting your file in there.
除了 --suffix (并不总是可用)之外的所有这些解决方案都有竞争条件。您可以通过使用 mktemp -d 创建一个目录,然后将您的文件放在那里来消除竞争条件。
##代码##回答by Roman Chernyatchik
MacOS Sierra 10.12 doesn't have --suffix option, so I suggest workaround:
MacOS Sierra 10.12 没有 --suffix 选项,所以我建议解决方法:
##代码##回答by Crestwave
Here's a more portable solution (POSIX-compatible):
这是一个更便携的解决方案(POSIX 兼容):
##代码##The first line runs mktempwithout creating the file, then sets tempto the generated filename with .psappended. The second line then creates it; touch "$temp"can be used instead if you prefer it.
第一行在mktemp不创建文件的情况下运行,然后设置temp为.ps附加的生成文件名。然后第二行创建它;touch "$temp"如果您愿意,可以改用它。
EDIT: Note that this doesn't have the same permissions as it creates it using shell redirection. If you need it to be unreadable by other users, you can use chmodto set it manually.
编辑:请注意,这与使用 shell 重定向创建它的权限不同。如果您需要其他用户无法读取它,您可以使用chmod手动设置它。

