shell编程 自动创建测试文件 升级版

相对于第一次写的shell编程,本次变动为:使用shell函数,使代码更简洁,使用trap捕捉信号,不许ctrl+c强制退出,其他无改进,代码如下:

trap "echo Error!!" SIGINT SIGTERM
echo $2|grep "[[:punct:]]" &> /dev/null && echo "Sorry! Filename only accept input(A-z or 0-9)" && exit 1
FTB=${2:-demo}
MaxArr=`ls ./|grep $FTB|grep -v ${FTB}.sh|cut -d"." -f1|awk -F"${FTB}" '{print $NF}'|sort -n|tail -1`
MaxFile=$((MaxArr+1))
Max=`ls ./|grep ${FTB}'[0-9][0-9]*'.sh|tail -1`
Havesame=`ls ./|grep -v ${FTB}'[0-9][0-9]*'.sh|grep ${FTB}`
Have1=`ls ./|grep ${FTB}|wc -l`
Create(){
        if [[ ! -f ./${FTB}.sh && -z $Max ]];then
                `touch ./${FTB}.sh;chmod 700 ./${FTB}.sh`
        #      echo "Create File ${FTB}.sh Success"
                echo -e '#!/bin/bash\n#Author: MOS\n'"#Date & Time: `date +%F/%T`\n#Version: 1.0.1\n#Description:\n#" > ./${FTB}.sh
                Res=`echo "${PWD}/${FTB}.sh"`
        elif [[ $Have1 = 1 && $Havesame = "${FTB}.sh" ]];then
                `touch ./${FTB}${Have1}.sh;chmod 700 ./${FTB}${Have1}.sh`
        #      echo "Create File ${FTB}${Have1}.sh Success"
                echo -e '#!/bin/bash\n#Author: MOS\n'"#Date & Time: `date +%F/%T`\n#Version: 1.0.1\n#Description:\n#" > ./${FTB}${Have1}.sh
                Res=`echo "${PWD}/${FTB}${Have1}.sh"`
        else
                  `touch ./${FTB}${MaxFile}.sh;chmod 700 ./${FTB}${MaxFile}.sh`
        #      echo "Create File ${FTB}${MaxFile}.sh Success"
                echo -e '#!/bin/bash\n#Author: MOS\n'"#Date & Time: `date +%F/%T`\n#Version: 1.0.1\n#Description:\n#" > ./${FTB}${MaxFile}.sh
                Res=`echo "${PWD}/${FTB}${MaxFile}.sh"`
        fi
}
if [[ -z $1 || $1 == "-c" && -n $2 ]];then
        Create
        echo $Res
elif [[ $1 = "-d" ]];then
        Create
        vim +$ $Res
        until bash -n $Res &> /dev/null ;do
        read -p "Syntax error!Are you sure you want to exit now?(y|n): " Quit
                if [[ $Quit == y ]];then
                        echo "Give up debug!!"
                        exit 1
                fi
        vim +$ $Res
        done
elif [[ $1 = "-m" ]];then
      Date=`date +%m-%d-%H:%M`
      mkdir -m 700 ./tmp$Date;mv *.sh ./tmp$Date
fi

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d003bc437253f2a90688e7dc9c105072.html