shell 脚本调试方法

http://blog.youkuaiyun.com/gsnumen/article/details/8004644

shell 脚本调试方法

 

如果自己写的shell脚本运行崩溃时, 有种方法能提示在 什么时候  哪个文件 的 哪个函数  哪行语句 出现了问题, 是不是会帮助自己更快解决问题呢? 

以下就是介绍这种方法:

 

1 将 mylog 放在 /etc 下

 

[cpp]  view plain copy
 
  1. #!/bin/bash  
  2. # ########################################################  
  3. # Filename: mylog  
  4. # Description: debug shell script  
  5. # Example:  
  6. #  
  7. #    #!/bin/bash  
  8. #    #file: xxx.sh  
  9. #     source /etc/mylog log debug error info notice warn echo  
  10. #     mylog_error "gaga gaga gagaga\n";  
  11. #  
  12. ##############################################   
  13.   
  14. #  Aliases  are  not  expanded  when  the  shell  is  not  interactive, unless the  
  15. #+ expand_aliases shell option is set using shopt  
  16. shopt -s expand_aliases  
  17.   
  18. OK="\033[32m OK \033[0m";  
  19. ERROR="\033[31m ERROR \033[0m";  
  20. FALSE="\033[31m FALSE \033[0m";  
  21. SUCCESS="[SUCCESS]";  
  22.   
  23. alias mylog='#';  
  24. alias mylog_debug='#';  
  25. alias mylog_error='#';  
  26. alias mylog_echo='#';  
  27. alias mylog_info='#';  
  28. alias mylog_notice='#';  
  29. alias mylog_warn='#';  
  30. alias mylog_error='#';  
  31.   
  32. for i in $@  
  33. do  
  34.     case "$i" in  
  35.         "log")  
  36.             alias mylog='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO ';  
  37.             ;;  
  38.         "info")  
  39.              alias mylog_info='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [INFO] ';  
  40.             ;;  
  41.         "notice")  
  42.              alias mylog_notice='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [NOTICE] ';  
  43.             ;;  
  44.         "warn")  
  45.              alias mylog_warn='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [WARN] ';  
  46.             ;;  
  47.         "debug")  
  48.              alias mylog_debug='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [DEBUG] ';  
  49.             ;;  
  50.         "error")  
  51.              alias mylog_error='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [ERROR] ';  
  52.             ;;  
  53.         "echo")  
  54.              alias mylog_echo='echo ';  
  55.             ;;  
  56.         *)  
  57.             ;;  
  58.     esac  
  59. done  



2 使用

 

[cpp]  view plain copy
 
  1. #!/bin/bash  
  2. # ########################################################  
  3. # Filename: checkrun.sh  
  4. # Description:  
  5. #   验证 mylog 的用法  
  6. # Example:  
  7. # ########################################################  
  8.   
  9.   
  10. source /etc/mylog log debug error info notice warn echo  
  11.   
  12. function check_apache()  
  13. {  
  14.     #xxx;  
  15.     mylog "normal running\n";  
  16. }  
  17.   
  18. function check_mysql()  
  19. {  
  20.     #xxx;  
  21.     mylog_debug "normal running\n";   
  22. }  
  23.   
  24. function check_nginx()  
  25. {  
  26.     #xxx;  
  27.     mylog_error "normal running\n";   
  28. }  
  29.   
  30. function check_squid()  
  31. {  
  32.     #xxx;  
  33.     mylog_info "normal running\n";   
  34. }  
  35.   
  36. function check_varnish()  
  37. {  
  38.     #xxx;  
  39.     mylog_notice "normal running\n";   
  40. }  
  41.   
  42. function check_tomcat()  
  43. {  
  44.     #xxx;  
  45.     mylog_warn "normal running\n";   
  46. }  
  47.   
  48. function check_myprog()  
  49. {  
  50.     #xxx;  
  51.     mylog_echo "normal running";   
  52. }  
  53.   
  54.   
  55.   
  56. function check_all()  
  57. {  
  58.     check_apache;  
  59.     check_mysql;  
  60.     check_nginx;  
  61.     check_squid;  
  62.     check_varnish;  
  63.     check_tomcat;  
  64.     check_myprog;  
  65. }  
  66.   
  67. check_all;  

 

 

3 运行结果 

[php]  view plain copy
 
  1. $> ./checkrun.sh   
  2. 2012-09-21 16:03:04 checkrun.sh check_apache() 15 normal running  
  3. 2012-09-21 16:03:04 checkrun.sh check_mysql() 21 [DEBUG] normal running  
  4. 2012-09-21 16:03:04 checkrun.sh check_nginx() 27 [ERROR] normal running  
  5. 2012-09-21 16:03:04 checkrun.sh check_squid() 33 [INFO] normal running  
  6. 2012-09-21 16:03:04 checkrun.sh check_varnish() 39 [NOTICE] normal running  
  7. 2012-09-21 16:03:04 checkrun.sh check_tomcat() 45 [WARN] normal running  
  8. normal running  

 

4 调试

你可以根据打印的日志信息定位错误, 如

 

[php]  view plain copy
 
  1. 2012-09-21 16:03:04 checkrun.sh check_nginx() 27 [ERROR] normal running  

 

是说: 在 2012-09-21 16:03:04 执行脚本 checkrun.sh 的 check_nginx() 函数 在第 27 行左右发生了错误.

 

Good luck.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值