shell脚本编写小助手--by lustlost

本文介绍了一款用于自动化创建bash脚本的工具,该工具具备检测同名文件、添加版权信息、语法检查及自动添加执行权限等功能。

功能:

1.自动检测否存在同名文件,存在则提示用户存在,并给出编辑,删除后创建,和退出3个选项

2.自动添加脚本版权信息(有自定义和默认2种方式)

3. 自动检测有无语法错误;如果有语法错误则提示用户是否退出,不退出则打开vim修改,直到修改正确为止

4.脚本编写成功后自动添加执行权限

用法:

执行脚本后面添加脚本名称即可

 


  1. #!/bin/bash  
  2. #:-------------CopyRight-------------   
  3. #:Name:lustlost - 1.0  
  4. #:Date:2012-7-17  
  5. #:Author:lustlost  
  6. #:Email:lustlost@foxmail.com 
  7.  
  8. 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
  9. [ "$OPERAT" == q ] && exit
  10. [ "$OPERAT" == e ] && vim $1
  11. [ "$OPERAT" == r ] && rm -rf $1
  12. else  
  13.     read -p "Customize the copyright information ? input y/n: " SELECT  
  14.         if [ "$SELECT" == y -o "$SELECT" == Y ]; then  
  15.             read -p "Version Number:" VER  
  16.             read -p "Author:" AUTHOR  
  17.             read -p "Email address:" EMAIL 
  18.  
  19.             cat > $1 << EOF  
  20. #!/bin/bash  
  21. #:-------------CopyRight-------------   
  22. #:Name:`basename $1` - $VER  
  23. #:Date:`date '+%Y-%M-%d'`  
  24. #:Author:$AUTHOR  
  25. #:Email:$EMAIL 
  26.  
  27. EOF 
  28.  
  29.             vim +8 $1  
  30.         else  
  31.             cat > $1 < EOF  
  32. #!/bin/bash  
  33. #:-------------CopyRight-------------   
  34. #:Name:`basename $1` - 1.0  
  35. #:Date:`date '+%Y-%M-%d'`  
  36. #:Author:Your Name  
  37. #:Email:Your Email Address 
  38.  
  39. EOF  
  40.             vim +8 $1  
  41.         fi  
  42. fi 
  43.  
  44. until bash -n $1 &> /dev/null; do  
  45.         read -p "Syntax error!! input Q/q to exit,other to edit it: " COM  
  46.         if [ $COM == Q -o $COM == q ]; then  
  47.             exit  
  48.         else  
  49.             vim $1  
  50.         fi  
  51. done 
  52. chmod +x $1
  53. echo "sh is create and it is right!!!" 

本文转自lustlost 51CTO博客,原文链接:http://blog.51cto.com/lustlost/933810,如需转载请自行联系原作者 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值