每次新建一個腳本的時候都,都要在腳本前面加上#!/bin/bash等一些固定格式的文本,這個bash腳本建立腳本能夠自動添加解釋器爲bash、日期做者等一些信息。而且在建立完成後自動檢查語法是否有錯誤,若是有錯誤它會幫你用vim打開,而且定位到出現語法問題的行。若是語法有錯誤,會強制不讓你退出。要退出按ctrl+c鍵。給若是建立是的bash腳本文件,會給此文件加執行權限。linux
使用方法:在此腳本後面跟上你要建立的文件名,文件名要以.sh結尾,不然不會看成腳本文件。shell
#./create.sh new_file.sh
vim
#!/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換行符不一樣形成的。windows