天天看點

shell腳本編寫小助手--by lustlost

功能:

1.自動檢測否存在同名檔案,存在則提示使用者存在,并給出編輯,删除後建立,和退出3個選項

2.自動添加腳本版權資訊(有自定義和預設2種方式)

3. 自動檢測有無文法錯誤;如果有文法錯誤則提示使用者是否退出,不退出則打開vim修改,直到修改正确為止

4.腳本編寫成功後自動添加執行權限

用法:

執行腳本後面添加腳本名稱即可

#!/bin/bash  

#:-------------CopyRight-------------   

#:Name:lustlost - 1.0  

#:Date:2012-7-17  

#:Author:lustlost  

#:Email:[email protected] 

if [ -e $1 ]; then  

read -p "file exists!!! input "e" to edit it, "d" to delete it and create a new file, "q" to quiet this operat: " OPERAT

[ "$OPERAT" == q ] && exit

[ "$OPERAT" == e ] && vim $1

[ "$OPERAT" == r ] && rm -rf $1

else  

    read -p "Customize the copyright information ? input y/n: " SELECT  

        if [ "$SELECT" == y -o "$SELECT" == Y ]; then  

            read -p "Version Number:" VER  

            read -p "Author:" AUTHOR  

            read -p "Email address:" EMAIL 

            cat <b>&gt;</b> $1 &lt;&lt; EOF  

#:Name:`basename $1` - $VER  

#:Date:`date '+%Y-%M-%d'`  

#:Author:$AUTHOR  

#:Email:$EMAIL 

EOF 

            vim +8 $1  

        else  

            cat &gt; $1 <b>&lt;</b> EOF  

#:Name:`basename $1` - 1.0  

#:Author:Your Name  

#:Email:Your Email Address 

EOF  

        fi  

fi 

until bash -n $1 &amp;&gt; /dev/null; do  

        read -p "Syntax error!! input Q/q to exit,other to edit it: " COM  

        if [ $COM == Q -o $COM == q ]; then  

            exit  

            vim $1  

done 

chmod +x $1

echo "sh is create and it is right!!!" 

本文轉自lustlost 51CTO部落格,原文連結:http://blog.51cto.com/lustlost/933810,如需轉載請自行聯系原作者