前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux shell创建临时文件

linux shell创建临时文件

作者头像
葫芦
发布2019-05-10 10:47:57
2.7K0
发布2019-05-10 10:47:57
举报
文章被收录于专栏:葫芦葫芦

[root@aoi ~]# cat d #!/bin/bash #creating and using a temp file tempfile=`mktemp wz19.XXXXXX` exec 3>$tempfile echo "This script write 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 [root@aoi ~]# sh d This script write to temp file wz19.gnxX9K Done creating temp file. The contents are: This is the first line This is the second line This is the last line [root@aoi ~]# ls -al wz19* ls: cannot access wz19*: No such file or directory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ mktemp -t wz.XXXXXX会将文件创建在系统临时文件夹下 [root@aoi ~]# mktemp -t wz.XXXXXX /tmp/wz.cs6mCq [root@aoi ~]# cat s #!/bin/bash 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 [root@aoi ~]# sh s The temp file is located at: /tmp/tmp.xpLNt9 This is a test file. This is the second line of the test. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [root@aoi dir.BEEbII5]# cat ../a #!/bin/bash tempdir=`mktemp -d dir.XXXXXXX` 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 [root@aoi dir.BEEbII5]# ll total 8 -rw-------. 1 root root 44 Nov 20 08:24 temp.D3JWPR -rw-------. 1 root root 44 Nov 20 08:24 temp.n0IElP [root@aoi dir.BEEbII5]# cat temp.D3JWPR This is a test line of data for temp.D3JWPR [root@aoi dir.BEEbII5]# cat temp.n0IElP This is a test line of data for temp.n0IElP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tee filename 它将从STDIN 过来的数据同时发给两个目的地。一个目的地是STDOUT一个是 tee命令指定的文件名 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [root@aoi dir.BEEbII5]# date | tee wz Wed Nov 20 08:27:54 CST 2013 [root@aoi dir.BEEbII5]# cat wz Wed Nov 20 08:27:54 CST 2013 [root@aoi dir.BEEbII5]# who | tee wz root pts/1 2013-11-20 03:18 (192.168.1.100) [root@aoi dir.BEEbII5]# cat wz root pts/1 2013-11-20 03:18 (192.168.1.100) 默认情况下tee命令会在每次使用时覆盖输出文件的内容 如果想将数据追加到文件中必须用-a选项 [root@aoi dir.BEEbII5]# date | tee -a wz Wed Nov 20 08:30:05 CST 2013 [root@aoi dir.BEEbII5]# cat wz root pts/1 2013-11-20 03:18 (192.168.1.100) Wed Nov 20 08:30:05 CST 2013 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash tempfile=test22file echo "This is the start of the test" | tee $tempfile echo "This is the second line of the test" | tee -a $tempfile echo "This is the end of the test" | tee -a $tempfile [root@aoi dir.BEEbII5]# sh v This is the start of the test This is the second line of the test This is the end of the test [root@aoi dir.BEEbII5]# cat test22file This is the start of the test This is the second line of the test This is the end of the test

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014/02/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档