mktemp 中文man页面

系统
mktemp 根据给定的文件名模板, 改变其中的一部分, 从而生成临时文件名. 该文件名是唯一的, 可以让其他程序使用. 模板为任意文件名, 后接六个 `X' 例如 /tmp/temp.XXXXXX 这些 `X' 将被 当前进程号 以及/或者某个唯一的字母组合替代.

名称 (NAME)

mktemp - 产生唯一的临时文件名  

总览 (SYNOPSIS)

mktemp [-q ] [-u ] template  

描述 (DESCRIPTION)

mktemp 根据给定的文件名模板, 改变其中的一部分, 从而生成临时文件名. 该文件名是唯一的, 可以让其他程序使用. 模板为任意文件名, 后接六个 `X' 例如 /tmp/temp.XXXXXX 这些 `X' 将被 当前进程号 以及/或者某个唯一的字母组合替代.

如果 成功 产生了 唯一文件名, 就 以访问模式 0600 (除非 使用了 -u 选项) 创建 文件, 并且 在 标准输出 显示 这个 文件名.

mktemp 用于 让 shell 脚本程序 使用 可靠的 临时文件. 多数 shell 程序 的 传统做法 是 程序名 加上 PID 做 后缀, 产生的文件名 就是 临时文件名. 这种 命名策略 容易 预测, 产生的 竞争条件 易于 遭到 攻击. 使用 相同 命名策略 的 另一个 方法 是 建立 临时目录, 这种 做法 相对 安全 一些. 它 可以 保证 临时文件 不被 破坏, 但是 容易 遭到 简单的 拒绝服务 攻击. 所以 建议 改用 mktemp

选项 (OPTIONS)

有效选项有:

-q
出错时 不显示 信息. 用于 禁止 错误信息 输出到 标准错误.
-u
以 ``不安全'' 模式 运行. 在 退出前 会 删除 临时文件. 它 比 Fn mktemp 3 稍微 好些, 但 仍然 会 引入 竞争条件. 不鼓励 使用 这个 选项.

返回值 (RETURN VALUES)

成功时 返回 0, 否则 返回 1.  

例子 (EXAMPLES)

下列的 sh(1) 片断 展示了 的 简单用法, 如果 无法 获得 可靠的 临时文件, 程序 就 退出.

TMPFILE=`mktemp /tmp/$0.XXXXXX` || exit 1
echo "program output" >> $TMPFILE

照上例, 我们 打算 让 脚本程序 自己 捕获 这个 错误.

TMPFILE=`mktemp -q /tmp/$0.XXXXXX`
if [ $? -ne 0 ]; then
        echo "$0: Can't create temp file, exiting..."
        exit 1
fi

另见 (SEE ALSO)

mkstemp(3), mktemp(3)  

#p#

NAME

mktemp - make temporary filename (unique)  

SYNOPSIS

mktemp [-V] | [-dqtu] [-p directory] [template]  

DESCRIPTION

The mktemp utility takes the given filename template and overwrites a portion of it to create a unique filename. The template may be any filename with some number of `Xs' appended to it, for example /tmp/tfile.XXXXXXXXXX. If no template is specified a default of tmp.XXXXXXXXXX is used and the -t flag is implied (see below).

The trailing `Xs' are replaced with a combination of the current process number and random letters. The name chosen depends both on the number of `Xs' in the template and the number of collisions with pre-existing files. The number of unique filenames mktemp can return depends on the number of `Xs' provided; ten `Xs' will result in mktemp testing roughly 26 ** 10 combinations.

If mktemp can successfully generate a unique filename, the file (or directory) is created with file permissions such that it is only readable and writable by its owner (unless the -u flag is given) and the filename is printed to standard output.

mktemp is provided to allow shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the PID as a suffix and use that as a temporary filename. This kind of naming scheme is predictable and the race condition it creates is easy for an attacker to win. A safer, though still inferior approach is to make a temporary directory using the same naming scheme. While this does allow one to guarantee that a temporary file will not be subverted, it still allows a simple denial of service attack. For these reasons it is suggested that mktemp be used instead.

The options are as follows:

-V
Print the version and exit.
-d
Make a directory instead of a file.
-p directory
Use the specified directory as a prefix when generating the temporary filename. The directory will be overridden by the user's TMPDIR environment variable if it is set. This option implies the -t flag (see below).
-q
Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error.
-t
Generate a path rooted in a temporary directory. This directory is chosen as follows:
*
If the user's TMPDIR environment variable is set, the directory contained therein is used.
*
Otherwise, if the -p flag was given the specified directory is used.
*
If none of the above apply, /tmp is used.

In this mode, the template (if specified) should be a directory component (as opposed to a full path) and thus should not contain any forward slashes.

-u
Operate in ``unsafe'' mode. The temp file will be unlinked before mktemp exits. This is slightly better than mktemp(3) but still introduces a race condition. Use of this option is not encouraged.

The mktemp utility exits with a value of 0 on success or 1 on failure.  

EXAMPLES

The following sh(1) fragment illustrates a simple use of mktemp where the script should quit if it cannot get a safe temporary file.

TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
echo "program output" >> $TMPFILE

The same fragment with support for a user's TMPDIR environment variable can be written as follows.

TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
echo "program output" >> $TMPFILE

This can be further simplified if we don't care about the actual name of the temporary file. In this case the -t flag is implied.

TMPFILE=`mktemp` || exit 1
echo "program output" >> $TMPFILE

In some cases, it may be desirable to use a default temporary directory other than /tmp. In this example the temporary file will be created in /extra/tmp unless the user's TMPDIR environment variable specifies otherwise.

TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
echo "program output" >> $TMPFILE

In some cases, we want the script to catch the error. For instance, if we attempt to create two temporary files and the second one fails we need to remove the first before exiting.

TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1
TMP2=`mktemp -t example.2.XXXXXXXXXX`
if [ $? -ne 0 ]; then
        rm -f $TMP1
        exit 1
fi

Or perhaps you don't want to exit if mktemp is unable to create the file. In this case you can protect that part of the script thusly.

TMPFILE=`mktemp -t example.XXXXXXXXXX` && {
        # Safe to use $TMPFILE in this block
        echo data > $TMPFILE
        ...
        rm -f $TMPFILE
}

ENVIRONMENT

TMPDIR
directory in which to place the temporary file when in -t mode

SEE ALSO

mkdtemp(3), mkstemp(3), mktemp(3)  

 

责任编辑:韩亚珊 来源: www.linuxforum.net/man-pages/
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:39:42

fcloseall中文man

2011-08-25 15:00:15

cfgetispeed中文man

2011-08-19 18:35:50

issue中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 17:40:25

setvbuf中文man

2011-08-23 14:21:16

poweroff中文man

2011-08-24 15:52:59

intro中文man

2011-08-23 13:40:31

2011-08-25 15:54:08

ferror中文man

2011-08-25 17:24:54

puts中文man

2011-08-25 18:34:55

ungetc中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-24 15:48:38

INSERT中文man
点赞
收藏

51CTO技术栈公众号