.\" $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