每次新建一个脚本的时候都,都要在脚本前面加上#!/bin/bash等一些固定格式的文本,这个bash脚本创建脚本可以自动添加解释器为bash、日期作者等一些信息。并且在创建完成后自动检查语法是否有错误,如果有错误它会帮你用vim打开,并且定位到出现语法问题的行。如果语法有错误,会强制不让你退出。要退出按ctrl+c键。给如果创建是的bash脚本文件,会给此文件加执行权限。

使用方法:在此脚本后面跟上你要创建的文件名,文件名要以.sh结尾,否则不会当作脚本文件。

    #./create.sh  new_file.sh

#!/bin/bash#### 判断文件是否存在Sh=$(echo $1 | grep -o  '.sh$')#if ! [ "$#" -eq 1  ]#then#	echo 'SYNOPSIS:./createfile.sh 
'# exit #fiif  [ -a "$1" ]then echo "file $1 is exists. pless input other filename" exit 1elif [ -d "$1" ]then echo "file $1 is directory. pless input other filename" exit 2#创建脚本文件elif [ ".sh" == "$Sh" ]then cat << EOF >>$1#!/bin/bash#description:#version:#author: wukui#license:GPLEOFDate=$(date +%F-%H:%M)echo "#date:$Date"  >>$1else touch $1 exitfi# 判断是否为bash脚本类型文件,如果是则打开并定位到最后一行if [ 'shell' == `file $1 | cut -d' ' -f3` ]then vim + $1else exitfi#给脚本文件加执行权限if [ 'shell' == `file $1 | cut -d' ' -f3` ]then if ! [ -x "$1" ] then echo "chmod +x $1" chmod u+x $1 &> /dev/null fifi#检查语法,如果有问题,用vim打开并定位到出现问题的行if [ 'shell' == `file $1 | cut -d' ' -f3` ]then if ! `bash -n $1` then touch aaaaaaaaaa bash -n $1 &> aaaaaaaaaa Line=$(cut -d: -f2 aaaaaaaaaa | cut -d' ' -f3 | sort -u) while [ -n "$Line"  ] do vim +$Line $1 bash -n $1 &> aaaaaaaaaa Line=$(cut -d: -f2 aaaaaaaaaa | cut -d' ' -f3 | sort -u) done else echo "very good" exit fifirm -f aaaaaaaaaa

附件用windows记事本打开可能会不换行。这是因linux与windoows换行符不同造成的。