shell基础——创建临时文件

时间:2023-03-09 02:01:53
shell基础——创建临时文件

(1)当前时间命名临时文件:

按照一定的格式获取当前时间:
date +%y-%m-%d_%H:%M:%S

#generate the file name
tmpfile=`date +%y-%m-%d_%H:%M:%S` #create a empty file
>$tmpfile #do what you want to do using the temp file
#...
# #delete temp file
rm -f $tmpfile

(2)当前进程ID命名临时文件:

获取当前进程ID:

$$

#generate the file name
tmpfile=$$ #create a empty file
>$tmpfile #do what you want to do using the temp file
#...
# #delete temp file
rm -f $tmpfile

(3)隐藏的临时文件

#generate the file name
tmpfile="temp" #Temp file middile name
prefix=. #In Linux env, hide the file use .
suffix=_$$ #Using process ID as the suffix of temp file name
tmpfile=${prefix}${tmpfile}${suffix} #create a empty file
>$tmpfile #do what you want to do using the temp file
#...
# #delete temp file
rm -f $tmpfile