您的位置:首页 > 运维架构 > Shell

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  建立临时目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell file