linux shell创建临时文件

葫芦的运维日志

下一篇 搜索 上一篇

2014/02/16 17:52


[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

葫芦的运维日志

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2019 王梓 赞助联系方式 陕ICP备17005322号