shell_60.Linux使用临时文件

使用临时文件
1.创建本地临时文件
在默认情况下,mktemp 会在本地目录中创建一个文件。在使用 mktemp 命令时,只需指定一个文件名模板即可。
模板可以包含任意文本字符,同时在文件名末尾要加上 6 个 X:

$ mktemp testing.XXXXXX 
$ ls -al testing* 
-rw------- 1 rich rich 0 Jun 20 21:30 testing.UfIi13
$

2.mktemp 命令会任意地将 6 个 X 替换为同等数量的字符,以保证文件名在目录中是唯一的。
你可以创建多个临时文件,并确保每个文件名都不重复:

$ mktemp testing.XXXXXX
testing.1DRLuV
$ mktemp testing.XXXXXX
testing.lVBtkW
$ mktemp testing.XXXXXX
testing.PgqNKG
$ ls -l testing*
-rw------- 1 rich rich 0 Jun 20 21:57 testing.1DRLuV
-rw------- 1 rich rich 0 Jun 20 21:57 testing.PgqNKG
-rw------- 1 rich rich 0 Jun 20 21:30 testing.UfIi13
-rw------- 1 rich rich 0 Jun 20 21:57 testing.lVBtkW
$

3.如你所见,mktemp 命令的输出正是它所创建的文件名。在脚本中使用 mktemp 命令时
可以将文件名保存到变量中,这样就能在随后的脚本中引用了:

$ cat test19
#!/bin/bash
# creating and using a temp file
tempfile=$(mktemp test19.XXXXXX)
exec 3>$tempfile
echo "This script writes to temp file $tempfile"
echo "This is the first line" >&3
echo "This is the second line." >&3
echo "This is the last line." >&3
exec 3>&-
echo "Done creating temp file. The contents are:"
cat $tempfile
rm -f $tempfile 2> /dev/null
$ ./test19
This script writes to temp file test19.vCHoya
Done creating temp file. The contents are:
This is the first line
This is the second line.
This is the last line.
$ ls -al test19*
-rwxr--r-- 1 rich rich 356 Jun 20 22:03 test19
$


该脚本使用 mktemp 命令创建了临时文件并将文件名赋给了$tempfile 变量。接下来将这
个临时文件作为文件描述符 3 的输出重定向文件。将临时文件名显示在 STDOUT 之后,向临时文件
中写入了几行文本,然后关闭了文件描述符。最后,显示临时文件的内容,用 rm 命令将其删除。


4.在/tmp 目录中创建临时文件
-t 选项会强制 mktemp 命令在系统的临时目录中创建文件。
在使用这个特性时,mktemp命令会返回所创建的临时文件的完整路径名,而不只是文件名:

$ mktemp -t test.XXXXXX 
/tmp/test.xG3374 
$ ls -al /tmp/test* 
-rw------- 1 rich rich 0 2020-06-20 18:41 /tmp/test.xG3374 
$ 

由于 mktemp 命令会返回临时文件的完整路径名,因此可以在文件系统的任何位置引用该临时文件:

$ cat test20 
#!/bin/bash 
# creating a temp file in /tmp 
tempfile=$(mktemp -t tmp.XXXXXX) 
echo "This is a test file." > $tempfile 
echo "This is the second line of the test." >> $tempfile 
echo "The temp file is located at: $tempfile" 
cat $tempfile 
rm -f $tempfile 
$ ./test20 
The temp file is located at: /tmp/tmp.Ma3390 
This is a test file. 
This is the second line of the test. 
$ 


在创建临时文件时,mktemp 会将全路径名返回给环境变量。这样就能在任何命令中使用该值来引用临时文件了


7.创建临时目录
-d 选项会告诉 mktemp 命令创建一个临时目录。你可以根据需要使用该目录,比如在其中
创建其他的临时文件:

$ cat test21 
#!/bin/bash 
# using a temporary directory 
tempdir=$(mktemp -d dir.XXXXXX) 
cd $tempdir 
tempfile1=$(mktemp temp.XXXXXX) 
tempfile2=$(mktemp temp.XXXXXX) 
exec 7> $tempfile1 
exec 8> $tempfile2 
echo "Sending data to directory $tempdir" 
echo "This is a test line of data for $tempfile1" >&7 
echo "This is a test line of data for $tempfile2" >&8 
$ ./test21 
Sending data to directory dir.ouT8S8 
$ ls -al 
total 72 
drwxr-xr-x 3 rich rich 4096 Jun 21 22:20 ./ 
drwxr-xr-x 9 rich rich 4096 Jun 21 09:44 ../ 
drwx------ 2 rich rich 4096 Jun 21 22:20 dir.ouT8S8/ 
-rwxr--r-- 1 rich rich 338 Jun 21 22:20 test21 
$ cd dir.ouT8S8 
[dir.ouT8S8]$ ls -al 
total 16 
drwx------ 2 rich rich 4096 Jun 21 22:20 ./ 
drwxr-xr-x 3 rich rich 4096 Jun 21 22:20 ../ 
-rw------- 1 rich rich 44 Jun 21 22:20 temp.N5F3O6 
-rw------- 1 rich rich 44 Jun 21 22:20 temp.SQslb7 
[dir.ouT8S8]$ cat temp.N5F3O6 
This is a test line of data for temp.N5F3O6 
[dir.ouT8S8]$ cat temp.SQslb7 
This is a test line of data for temp.SQslb7 
[dir.ouT8S8]$ 

这段脚本在当前目录中创建了一个临时目录,然后使用 cd 命令进入该目录,在其中创建了两个临时文件。
这两个临时文件又被分配给了文件描述符以用来保存脚本的输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微辣已是极限

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值