Advice on how to set up error handling

Hi Folks -

I want to add error handling to a portion of a *.ksh, but I'm having difficulty doing so in an easily digestible way.

Essentially, I want to echo weather it was successful or unsuccessful after each command.

Here is the code I need to add error handling to:

perl 2passdatechange.pl
sleep 60
startMaxl.sh oracle.mxl

chmod -R 775 *.*
MY_DATE_STRING=`date +%a`
if [ $MY_DATE_STRING == "Sun" ]
then
    startMaxl.sh dly05loadto_INP_cslawr.mxl
    sleep 1800
    ./sunday_cslawr.ksh
else
    sleep 60
fi

startMaxl.sh ascmon.mxl

I did add one additional step. So before the above section of code, the script tries to locate 4 text files. If any of the text fiels are not found, it should not execute the remaining commands. I've tried to capture that below as you'll see.

Below is what I tried to spin up - but as you can see, looks awfully long and unnecessary.

#::-- Scripts will not  execute if missing a AWI/GSC Data File --::#
if [[ ! "${_ERR}" = "T" ]]
then
    cd "${_DAILYSALES_BIN}"

    perl 2passdatechange.pl
    sleep 60
    startMaxl.sh Oracle_PEL.mxl $MMDD1

    _RVAL=$?
    if [ $_RVAL -eq 0 ]
    then
        echo ---------------------------------------------------------
        echo "Oracle_PEL.mxl Process : Successful"                           
        echo ---------------------------------------------------------
    else
        echo ---------------------------------------------------------
        echo "Oracle_PEL.mxl Process  Unsuccessful"                      
        echo ---------------------------------------------------------
        _ERR=T
    fi
else
    echo "1 or more AWI or GSC data file(s) does not exist"
    echo "No further processing will be executed"
fi

if [[ ! "${_ERR}" = "T" ]]
then
    chmod -R 775 *.*
    MY_DATE_STRING=`date +%a`
    if [ $MY_DATE_STRING == "Sun" ]
    then
        startMaxl.sh dly05loadto_INP_cslawr.mxl
        
        _RVAL=$?
        if [ $_RVAL -eq 0 ]
        then
            echo ---------------------------------------------------------
            echo "dly05loadto_INP_cslawr.mxl Process : Successful"                           
            echo ---------------------------------------------------------
                sleep 1800
                ./sunday_cslawr.ksh
                
                _RVAL=$?
                if [ $_RVAL -eq 0 ]
                then
                    echo ---------------------------------------------------------
                    echo "sunday_cslawr.ksh Process : Successful"                           
                    echo ---------------------------------------------------------
                else
                    echo ---------------------------------------------------------
                    echo "sunday_cslawr.ksh Process : Successful"                           
                    echo ---------------------------------------------------------
                    _ERR=T
                fi
        else
            echo ---------------------------------------------------------
            echo "dly05loadto_INP_cslawr.mxl Process  Unsuccessful"                      
            echo ---------------------------------------------------------
            _ERR=T
        fi

    else
        sleep 60
    fi
    startMaxl.sh ascmon.mxl
        _RVAL=$?
    if [ $_RVAL -eq 0 ]
    then
        echo ---------------------------------------------------------
        echo "ascmon.mxl Process : Successful"                           
        echo ---------------------------------------------------------
    else
        echo ---------------------------------------------------------
        echo "ascmon.mxl Process  Unsuccessful"                      
        echo ---------------------------------------------------------
        _ERR=T
    fi
else 
    ""
fi

Any tips would be greatly appreciated!

You might define and deploy an "error handler" function to be called after every single command. Did you consider the DEBUG trap ?

Hi Rudi -

I will check that out now!

Here is what I have working for the time being - works fine. I will look at DEBUG.

echo ---------------------------------------------------------                                                                                                
echo "Execute Data Import & Export Procedures"                                         
echo ---------------------------------------------------------

#::-- Scripts will not  execute if missing a AWI/GSC Data File --::#
if [[ ! "${_ERR}" = "T" ]]
then
    cd "${_DAILYSALES_BIN}"

    perl 2passdatechange.pl 
    sleep 60
    startMaxl.sh oracle_PEL.mxl $MMDD1
    
        _RVAL=$?
        if [ $_RVAL -eq 0 ]
        then
            echo ---------------------------------------------------------                                                                                                
            echo "Oracle_PEL.mxl : Successful"                                         
            echo ---------------------------------------------------------
        else
            echo ---------------------------------------------------------                                                                                                
            echo "Oracle_PEL.mxl : Unsuccessful"                                         
            echo ---------------------------------------------------------
            _ERR=T
        fi

    if [[ ! "${_ERR}" = "T" && ${DDD} == "Sun" ]]
    then
        echo chmod -R 775 *.*
        startMaxl.sh dly05loadto_INP_cslawr.mxl
        sleep 1800
        
        _RVAL=$?
        if [ $_RVAL -eq 0 ]
        then
            echo ---------------------------------------------------------                                                                                                
            echo "dly05loadto_INP_cslawr.mxl : Successful"                                         
            echo ---------------------------------------------------------
        else
            echo ---------------------------------------------------------                                                                                                
            echo "dly05loadto_INP_cslawr.mxl : Unsuccessful"                                         
            echo ---------------------------------------------------------
            _ERR=T
        fi
            
        if [[ ! "${_ERR}" = "T" ]]
        then
        ./sunday_cslawr.ksh

            _RVAL=$?
            if [ $_RVAL -eq 0 ]
            then
                echo ---------------------------------------------------------                                                                                                
                echo "sunday_cslawr.ksh : Successful"                                         
                echo ---------------------------------------------------------
            else
                echo ---------------------------------------------------------                                                                                                
                echo "sunday_cslawr.ksh : Unsuccessful"                                         
                echo ---------------------------------------------------------
                _ERR=T
            fi
        fi
        
    fi
    sleep 60
    if [[ ! "${_ERR}" = "T" ]]
    then
    startMaxl.sh ascmon.mxl
        
        _RVAL=$?
        if [ $_RVAL -eq 0 ]
        then
            echo ---------------------------------------------------------                                                                                                
            echo "ascmon.mxl : Successful"                                         
            echo ---------------------------------------------------------
        else
            echo ---------------------------------------------------------                                                                                                
            echo "ascmon.mxl : Unsuccessful"                                         
            echo ---------------------------------------------------------
            _ERR=T
        fi
    fi
else
    echo "1 or more AWI or GSC data file(s) does not exist"
    echo "No further processing will be executed"
fi