1. 程式人生 > >shell指令碼除錯 -- 執行日誌1

shell指令碼除錯 -- 執行日誌1

# 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 $?