.\" $OpenBSD: mktemp.1,v 1.10 1998/09/26 19:55:09 aaron Exp $ .\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .Dd November 20, 1996 .Os .Dt MKTEMP 1 .Sh NAME .Nm mktemp .Nd 产生唯一的临时文件名 .Sh "总览 (SYNOPSIS)" .Nm mktemp .Op Fl q .Op Fl u .Ar template .Sh "描述 (DESCRIPTION)" .Nm mktemp 根据 给定的 文件名模板, 改变 其中的 一部分, 从而 生成 临时文件名. 该文件名 是 唯一的, 可以 让 其他程序 使用. 模板 为 任意 文件名, 后接 六个 .Ql X 例如 .Pa /tmp/temp.XXXXXX . 这些 .Ql X 将被 当前进程号 以及/或者 某个 唯一的 字母组合 替代. .Pp 如果 .Nm 成功 产生了 唯一文件名, 就 以 访问模式 0600 (除非 使用了 .Fl u 选项) 创建 文件, 并且 在 标准输出 显示 这个 文件名. .Pp .Nm mktemp 用于 让 shell 脚本程序 使用 可靠的 临时文件. 多数 shell 程序 的 传统做法 是 程序名 加上 PID 做 后缀, 产生的 文件名 就是 临时文件名. 这种 命名策略 容易 预测, 产生的 竞争条件 易于 遭到 攻击. 使用 相同 命名策略 的 另一个 方法 是 建立 临时目录, 这种 做法 相对 安全 一些. 它 可以 保证 临时文件 不被 破坏, 但是 容易 遭到 简单的 拒绝服务 攻击. 所以 建议 改用 .Nm mktemp . .Sh "选项 (OPTIONS)" .Bl -tag -width indent 有效选项有: .It Fl q 出错时 不显示 信息. 用于 禁止 错误信息 输出到 标准错误. .It Fl u 以 .Dq 不安全 模式 运行. .Nm 在 退出前 会 删除 临时文件. 它 比 .Fn mktemp 3 稍微 好些, 但 仍然 会 引入 竞争条件. 不鼓励 使用 这个 选项. .El .Sh "返回值 (RETURN VALUES)" .Nm 成功时 返回 0, 否则 返回 1. .Sh "例子 (EXAMPLES)" 下列的 .Xr sh 1 片断 展示了 .Nm 的 简单用法, 如果 无法 获得 可靠的 临时文件, 程序 就 退出. .Bd -literal -offset indent TMPFILE=`mktemp /tmp/$0.XXXXXX` || exit 1 echo "program output" >> $TMPFILE .Ed .Pp 照上例, 我们 打算 让 脚本程序 自己 捕获 这个 错误. .Bd -literal -offset indent TMPFILE=`mktemp -q /tmp/$0.XXXXXX` if [ $? -ne 0 ]; then echo "$0: Can't create temp file, exiting..." exit 1 fi .Ed .Sh "另见 (SEE ALSO)" .Xr mkstemp 3 , .Xr mktemp 3 .Sh "历史 (HISTORY)" 源于 .Bx Open .Sh "[中文版维护人]" 徐明 .Sh "[中文版最新更新]" 2003/05/13 .Sh "《中国Linux论坛man手册页翻译计划》" http://cmpp.linuxforum.net .Sh "跋" .br 本页面中文版由中文 man 手册页计划提供。 .br 中文 man 手册页计划:\fBhttps://github.com/man-pages-zh/manpages-zh\fR