#!/bin/bash# -t选项:强制mktemp命令在系统的临时目录中创建文件。# 使用该特性时,mktemp命令会返回所创建的临时文件的完整路径名。echo"******* mktemp -t test.XXXXXX *********"
mktemp -t test.XXXXXX
echo"******* ls -al /tmp/test* **********"ls-al /tmp/test.*
# 由于mktemp命令会返回临时文件的完整路径名,因此,可以在文件系统的任何位置引用该临时文件# creating a temp file in /tmptempfile=$(mktemp -t tmp.XXXXXX)echo"this is a test file.">$tempfileecho"this is the second line of the test.">>$tempfileecho"the temp file is located at: $tempfile"echo"**** cat $tempfile ****"cat$tempfilerm-f$tempfile
运行结果
******* mktemp -t test.XXXXXX *********
/tmp/test.Mhvh77
******* ls-al /tmp/test* **********
-rw------- 1 root root 0 Jul 2516:05 /tmp/test.Mhvh77
the temp file is located at: /tmp/tmp.9GcwMW
**** cat /tmp/tmp.9GcwMW ****
this is a test file.
this is the second line of the test.