您的位置:首页 > 运维架构 > Shell

shell 脚本调试方法

2012-09-21 15:59 751 查看
shell 脚本调试方法

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

以下就是介绍这种方法:

1 将 mylog 放在 /etc 下

#!/bin/bash
# ########################################################
# Filename: mylog
# Description: debug shell script
# Example:
#
#    #!/bin/bash
#    #file: xxx.sh
#     source /etc/mylog log debug error info notice warn echo
#     mylog_error "gaga gaga gagaga\n";
#
############################################## 

#  Aliases  are  not  expanded  when  the  shell  is  not  interactive, unless the
#+ expand_aliases shell option is set using shopt
shopt -s expand_aliases

OK="\033[32m OK \033[0m";
ERROR="\033[31m ERROR \033[0m";
FALSE="\033[31m FALSE \033[0m";
SUCCESS="[SUCCESS]";

alias mylog='#';
alias mylog_debug='#';
alias mylog_error='#';
alias mylog_echo='#';
alias mylog_info='#';
alias mylog_notice='#';
alias mylog_warn='#';
alias mylog_error='#';

for i in $@
do
    case "$i" in
        "log")
            alias mylog='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO ';
            ;;
        "info")
             alias mylog_info='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [INFO] ';
            ;;
        "notice")
             alias mylog_notice='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [NOTICE] ';
            ;;
        "warn")
             alias mylog_warn='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [WARN] ';
            ;;
        "debug")
             alias mylog_debug='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [DEBUG] ';
            ;;
        "error")
             alias mylog_error='echo -ne $(/bin/date +%Y-%m-%d) $(/bin/date +%H:%M:%S) $(basename $0) $FUNCNAME\(\) $LINENO [ERROR] ';
            ;;
        "echo")
             alias mylog_echo='echo ';
            ;;
        *)
            ;;
    esac
done


2 使用

#!/bin/bash
# ########################################################
# Filename: checkrun.sh
# Description:
#   验证 mylog 的用法
# Example:
# ########################################################

source /etc/mylog log debug error info notice warn echo

function check_apache()
{
    #xxx;
    mylog "normal running\n";
}

function check_mysql()
{
    #xxx;
    mylog_debug "normal running\n"; 
}

function check_nginx()
{
    #xxx;
    mylog_error "normal running\n"; 
}

function check_squid()
{
    #xxx;
    mylog_info "normal running\n"; 
}

function check_varnish()
{
    #xxx;
    mylog_notice "normal running\n"; 
}

function check_tomcat()
{
    #xxx;
    mylog_warn "normal running\n"; 
}

function check_myprog()
{
    #xxx;
    mylog_echo "normal running"; 
}

function check_all()
{
    check_apache;
    check_mysql;
    check_nginx;
    check_squid;
    check_varnish;
    check_tomcat;
    check_myprog;
}

check_all;


3 运行结果

$> ./checkrun.sh 
2012-09-21 16:03:04 checkrun.sh check_apache() 15 normal running
2012-09-21 16:03:04 checkrun.sh check_mysql() 21 [DEBUG] normal running
2012-09-21 16:03:04 checkrun.sh check_nginx() 27 [ERROR] normal running
2012-09-21 16:03:04 checkrun.sh check_squid() 33 [INFO] normal running
2012-09-21 16:03:04 checkrun.sh check_varnish() 39 [NOTICE] normal running
2012-09-21 16:03:04 checkrun.sh check_tomcat() 45 [WARN] normal running
normal running


4 调试

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

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: