每次新建一个脚本的时候都,都要在脚本前面加上#!/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 <FileName>'
#	exit 
#fi
if  [ -a "$1" ]
then
	echo "file $1 is exists. pless input other filename"
	exit 1
elif [ -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:GPL
EOF
Date=$(date +%F-%H:%M)
echo "#date:$Date"  >>$1
else
	touch $1
	exit
fi
# 判断是否为bash脚本类型文件,如果是则打开并定位到最后一行
if [ 'shell' == `file $1 | cut -d' ' -f3` ]
then
	vim + $1
else
	exit
fi

#给脚本文件加执行权限
if [ 'shell' == `file $1 | cut -d' ' -f3` ]
then
	if ! [ -x "$1" ]
	then
		echo "chmod +x $1"
		chmod u+x $1 &> /dev/null
	
	fi
fi
#检查语法,如果有问题,用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
	fi
fi
rm -f aaaaaaaaaa


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