shell 创建临时目录和临时文件mktemp
2011-11-25 08:52
246 查看
#!/bin/bash
mktemp_local()
{
tempfile=`mktemp temp.XXXXXX` #文件名格式 file.XXXXXX 6个大写X
exec 3>$tempfile
echo -e "\nThis script writes to local temp file $tempfile\n"
echo "This is first line." >&3
echo "This is second line." >&3
echo "This is third line." >&3
echo "Done creating tempfile.The contents are:"
cat $tempfile
rm -f $tempfile 2>/dev/null
}
mktemp_sys()
{
tempfile=`mktemp -t temp.XXXXXX` #在系统临时目录下创建临时文件
exec 3>$tempfile
echo -e "\nThis script writes to sys temp file $tempfile\n"
echo "This is first line." >&3
echo "This is second line." >&3
echo "This is third line." >&3
echo "Done creating tempfile.The contents are:"
cat $tempfile
rm -f $tempfile 2>/dev/null
}
mktemp_dir()
{
tempdir=`mktemp -d ./tmpd.XXXXXX` #创建临时目录
cd $tempdir
tempf_a=`mktemp tempf.XXXXXX` #创建本地临时文件
tempf_b=`mktemp tempf.XXXXXX`
exec 7>$tempf_a
exec 8>$tempf_b
echo "This is a test file for temp file:$tempf_a" >&7
echo "This is a test file for temp file:$tempf_b" >&8
echo -e "\nTempdir contents:\n"
echo "$tempf_a contents:"
cat $tempf_a
echo "$tempf_b contents:"
cat $tempf_b
cd - #返回原来的路径
echo -e "\nThe tempd is here:"
ls -d $tempdir
echo -e "\nrm the tempd is here:"
rm $tempdir -rf 2>/dev/null
}
mktemp_local
mktemp_sys
mktemp_dir
#要点
mktemp 命令及选项
没有选项时建立本地临时文件
-t 在系统的临时目录/tmp 下建立临时文件
-d 建立临时目录
mktemp_local()
{
tempfile=`mktemp temp.XXXXXX` #文件名格式 file.XXXXXX 6个大写X
exec 3>$tempfile
echo -e "\nThis script writes to local temp file $tempfile\n"
echo "This is first line." >&3
echo "This is second line." >&3
echo "This is third line." >&3
echo "Done creating tempfile.The contents are:"
cat $tempfile
rm -f $tempfile 2>/dev/null
}
mktemp_sys()
{
tempfile=`mktemp -t temp.XXXXXX` #在系统临时目录下创建临时文件
exec 3>$tempfile
echo -e "\nThis script writes to sys temp file $tempfile\n"
echo "This is first line." >&3
echo "This is second line." >&3
echo "This is third line." >&3
echo "Done creating tempfile.The contents are:"
cat $tempfile
rm -f $tempfile 2>/dev/null
}
mktemp_dir()
{
tempdir=`mktemp -d ./tmpd.XXXXXX` #创建临时目录
cd $tempdir
tempf_a=`mktemp tempf.XXXXXX` #创建本地临时文件
tempf_b=`mktemp tempf.XXXXXX`
exec 7>$tempf_a
exec 8>$tempf_b
echo "This is a test file for temp file:$tempf_a" >&7
echo "This is a test file for temp file:$tempf_b" >&8
echo -e "\nTempdir contents:\n"
echo "$tempf_a contents:"
cat $tempf_a
echo "$tempf_b contents:"
cat $tempf_b
cd - #返回原来的路径
echo -e "\nThe tempd is here:"
ls -d $tempdir
echo -e "\nrm the tempd is here:"
rm $tempdir -rf 2>/dev/null
}
mktemp_local
mktemp_sys
mktemp_dir
#要点
mktemp 命令及选项
没有选项时建立本地临时文件
-t 在系统的临时目录/tmp 下建立临时文件
-d 建立临时目录
相关文章推荐
- Linux:使用mktemp创建临时文件/目录
- mktemp命令_Linux mktemp 命令用法详解:创建临时文件供shell脚本使用
- mktemp -创建一个临时文件或目录
- 无法创建临时 il 文件;清理临时目录中的旧 il 文件
- 第4章 处理用户输入与显示数据------------(创建本地临时文件、在/temp中创建临时文件、创建临时目录、tee命令)
- shell基础——创建临时文件
- SHELL中创建临时文件的方法
- Linux下使用Shell根据txt文件批量创建目录
- shell递归创建目录和文件
- JAVA实现单个文件,目录以及临时文件的创建工具类
- 创建一个写入流写入一个文件到程序的临时目录里
- shell创建临时文件
- 第4章 处理用户输入与显示数据------------(创建本地临时文件、在/temp中创建临时文件、创建临时目录、tee命令)
- SQL SERVER 2008复制--代理消息代码 21100。 分发代理无法在“C:\Program Files\Microsoft SQL Server\100\COM”目录中创建临时文件。系统返回的错误代码为 5。
- 创建临时文件 临时目录
- 第4章 处理用户输入与显示数据------------(创建本地临时文件、在/temp中创建临时文件、创建临时目录、tee命令)
- shell 中创建临时文件(随机文件名)的方法总结
- 创建临时文件——tmpnam,tmpfile,mktemp,mkstemp
- shell模板-跨目录执行,彩色输出,临时文件,行遍历文件
- java 创建文件-创建目录-创建临时文件