歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

一個shell腳本編寫小助手

功能:

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 > $1 << EOF     
#!/bin/bash     
#:-------------CopyRight-------------      
#:Name:`basename $1` - $VER     
#:Date:`date '+%Y-%M-%d'`     
#:Author:$AUTHOR     
#:Email:$EMAIL    
        
EOF    
        
            vim +8 $1     
        else 
            cat > $1 < EOF     
#!/bin/bash     
#:-------------CopyRight-------------      
#:Name:`basename $1` - 1.0     
#:Date:`date '+%Y-%M-%d'`     
#:Author:Your Name     
#:Email:Your Email Address    
        
EOF     
            vim +8 $1     
        fi     
fi    
        
until bash -n $1 &> /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     
        else 
            vim $1     
        fi     
done    
chmod +x $1   
echo "sh is create and it is right!!!"

本文出自 “lustlost-迷失在欲望之中” 博客,請務必保留此出處http://lustlost.blog.51cto.com/2600869/933810

Copyright © Linux教程網 All Rights Reserved