How to create a test file automatically using shell programming

Source: Internet
Author: User
Tags exit chmod current time

Learn shell programming, every time to do a test, always create a file, give permission, file each edit also write a #!/bin/bash, when the test files are more, still have to organize, or move, or delete ... Kiss, don't bother ...

Here are some of the features that the script implements: (assuming the script is in the current directory and the name is test.sh)

1./test.sh without any parameters-> automatically create a file in the current directory with a name of test.sh, with a permission of 700, and multiple executions will be based on the original file +1, for example, the current directory already has test.sh, then created as test1.sh. Next time Test2 ... If a file in the current directory is test30.sh, it is created as a test31.sh. In turn, after the script is created, it automatically uses VIM to enter the edit

1.1./test.sh-c filename This does not explain, you can try Ha

2./test.sh-d into the debug mode, the creation of files, direct access, and the new file if there is a syntax error, will organize its regular exit, until there is no error or hint error when entering lowercase y can

3./test.sh-m to create a directory at the current time; throw all the files in the current directory at the end of. Sh. Actually, this feature item is better, such as selecting a path, for example, when moving, you can choose which keyword to start ... Helpless time is not enough, to wait for the next time to do it again.

3./test.sh-s Create sed script file

The code is as follows:

#!/bin/bash #Author: MOS #Script name:test.sh #Date & time:2012-09-26/21:44:16 #Version: 1.0.4 #Descripti On: #这个被推荐了好久, simply modify it directly, this script is not the oldest trap "echo error!!" SIGINT sigterm echo $2|grep "[[:p UNCT:]]" &>/dev/null && Echo sorry!  Filename only accept input (A-Z or 0-9) "&& exit 1 ftb=${2:-test} 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 ' Echoinfo () { Echo-e ' #!/bin/bash\n#author:mos\n ' "#Script name: ' basename $filename ' \n#date & Time: ' Date +%f/%t ' \n#version  : 1.0.1\n#description:\n# "> $filename} sedinfo () {echo-e ' #!/bin/sed-f\n#author:mos\n '" #Script name: ' basename $filename ' \n#date & Time: ' Date +%f/%t ' \n#version:1.0.1\n#description:\n# ' > $filename} headif (){[["$Select" = "s"]] && sedinfo | | echoinfo}  
                Create () {if [[!-F./${ftb}.sh &&-Z $Max]];then filename= ' mktemp./${ftb}.sh ' ' chmod $filename ' headif elif [[$Have 1 = 1 && $Havesame = ' ${ftb}.sh  
                "]];then filename= ' mktemp./${ftb}${have1}.sh ' chmod $filename '  
                Headif else Filename= ' mktemp./${ftb}${maxfile}.sh ' chmod $filename ' Headif fi} if [[Z $ | | | $ = = &&-N $]];then Create vim + $ $filename elif [[$ = ' d ']];then Create vim +$ $filename until Bash-n $filename && Gt /dev/null;d o read-p "Syntax error! Are you sure your want to exit now?
        (y|n): "Quit if [[$Quit = y]];then echo" Give Up debug!! "                Exit 1 fi vim +$ $filename done elif [[$ = "M"]];then Date= ' Date +%m-%d-%h:%m ' mkdir-m/tmp$date;mv *.sh./tmp$date elif [[$ = "-S"]];then select=
        Create vim +$ $filename else echo "error!invalid option." Exit 1 fi

By the way, if you use the./test1.sh-c filename, filename does not write symbols, although the script can be judged, but seems to judge () ' & is not normal ... This is not yet resolved, great God, give a thought ...

Well, also, personal English sucks ... All some variables named ... Uh.. I hereby declare: absolute modesty accepts any contempt ... and study hard, try to correct ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.