shell脚本调试 -- 运行日志1
模块文件: mymod_log
# mymod_log
while [[ ! -z "$1" ]]; do # echo "arg: $1"; case "$1" in "noall") sed -e '/^\([ \t]*\)mydebug/d'\ -e '/^\([ \t]*\)myrunlog/d'\ -e '/^\([ \t]*\)myerrlog/d'\ -e 's/^\([ \t]*\)\<source\>\(.*\)\<mymod_log\>\(.*\)$/#\1source\2mymod_log\3/g'\ "$0" |/bin/bash exit $? ;; "nodebug") #mydebug delete sed_rule="${sed_rule} -e '/^\([ \t]*\)mydebug/d'" ;; "norunlog") #myrunlog delete sed_rule="${sed_rule} -e '/^\([ \t]*\)myrunlog/d'" ;; "noerrlog") #myerrlog delete sed_rule="${sed_rule} -e '/^\([ \t]*\)myerrlog/d'" ;; *) ;; esac shift; done if [[ -z "${sed_rule}" ]];then sed -e 's/^\([ \t]*\)mydebug/\1echo "$FUNCNAME:$LINENO:DEBUG: "/g'\ -e 's/^\([ \t]*\)myrunlog/\1echo "$FUNCNAME:$LINENO:RUN: "/g'\ -e 's/^\([ \t]*\)myerrlog/\1echo 1>\&2 "$FUNCNAME:$LINENO:ERROR: "/g'\ -e 's/^\(.*\)\<source\>\(.*\)\<mymod_log\>\(.*\)$/#\1source\2mymod_log\3/g'\ "$0" |/bin/bash else # echo "${sed_rule}"; eval "sed ${sed_rule} $0" |\ sed 's/^\([ \t]*\)\<source\>\(.*\)\<mymod_log\>\(.*\)$/#\1source\2mymod_log\3/g'|\ sed -e 's/^\([ \t]*\)mydebug/\1echo "$FUNCNAME:$LINENO:DEBUG: "/g'\ -e 's/^\([ \t]*\)myrunlog/\1echo "$FUNCNAME:$LINENO:RUN: "/g'\ -e 's/^\([ \t]*\)myerrlog/\1echo 1>\&2 "$FUNCNAME:$LINENO:ERROR: "/g'\ | /bin/bash fi exit $? |
测试文件: mymod_log_test.sh
#!/bin/bash
# mymod_log_test.sh source ./mymod_log norunlog; function check_apache() { mydebug "apache [OK]"; myrunlog "apache [OK]"; myerrlog "apache [FAIL]"; :; } function check_mysql() { mydebug "mysql [OK]"; myrunlog "mysql [OK]"; myerrlog "mysql [FAIL]"; :; } function check_all() { check_apache; check_mysql; } check_all; |
测试结果:
当为: source ./mymod_log norunlog; 时
$ ./mymod_log_test.shcheck_apache:8:DEBUG: apache [OK]check_apache:9:ERROR: apache [FAIL]check_mysql:16:DEBUG: mysql [OK]check_mysql:17:ERROR: mysql [FAIL]
当为 source ./mymod_log norunlog noerrlog;
$ ./mymod_log_test.shcheck_apache:8:DEBUG: apache [OK]check_mysql:15:DEBUG: mysql [OK]
当为: source ./mymod_log noall;
$ ./mymod_log_test.sh$
------------- end -------------
From: GS
-------------------------------
From: GS
-------------------------------