Advice on how to set up error handling

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Advice on how to set up error handling
# 1  
Old 10-19-2017
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:

Code:
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.

Code:
#::-- 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!
# 2  
Old 10-20-2017
You might define and deploy an "error handler" function to be called after every single command. Did you consider the DEBUG trap?
# 3  
Old 10-20-2017
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.

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

New advice on error checking

HI All, Whenever I write a shell script I always check if a command got executed successfully, even for the commands like cd, mv, rm and others, and even for the cases when there is ALMOST nothing to stop this commands from executing with success. so I am wondering if it is an overkill. I am... (1 Reply)
Discussion started by: rdogadin
1 Replies

2. Shell Programming and Scripting

Error handling for file

Hi Guys, I got a csv with pipe delimted file and i want to check second column of the file has any alpha character becuase I am expecting only number in that, and if any alpha characters then it should throw an error Thanks in advance (1 Reply)
Discussion started by: Rizzu155
1 Replies

3. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

4. Shell Programming and Scripting

Error Handling

Below code works for different databases i.e. MYSQL and ORACLE The problem is for MYSQL in Block: if ; $? taking value accordingly but in case of ORACLE $? is always taking this value as zero (0). That is the reason in Oracle it always going in else Block in any case.. :( and in case of ... (4 Replies)
Discussion started by: ambarginni
4 Replies

5. Red Hat

Advice regarding filesystems handling large number of files

Hi All, I have a CentOS operating system installed. I work with really huge number of files which are not only huge in number but some of them really huge in size. Minimum number of files could be 1 million to 2 million in one directory itself. Some of the files are even several Gigabytes in... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. Shell Programming and Scripting

set -o vi giving set: Syntax error

when trying : set -o vi getting error like- : set: Syntax error help me Double post (of sorts). Continue here. (0 Replies)
Discussion started by: RahulJoshi
0 Replies

7. Programming

C - advice how to catch some weird error

I have some unstable mistake in my program and out-of-idea how to catch it. I am looking for advice with a way to work it out! I have in a pretty complicated program (but one source file) set of int-counters - 15, if exactly. Lately, on final printout I have inpossible value (I am... (3 Replies)
Discussion started by: alex_5161
3 Replies

8. Shell Programming and Scripting

Error Handling

Helo Experts, I need a help in handling errors in shell script, wants my errors displayed in text file instead of command window.. My shell script is here; cd /cygdrive/s/Files for FILES in ./*.* do temp=`basename $FILES` if cp $FILES /cygdrive/r/CopyFile1/$FILES; then echo "copy... (5 Replies)
Discussion started by: CelvinSaran
5 Replies

9. Shell Programming and Scripting

SFTP Error Handling

Hi , Can any one tell me is there any standard method to track errors during sftp ? using which command i can track sftp errors ? i tried using echo $? . Most of the times i am getting error number 127 ,1, 255. whether it is constant numbers ? Please help me out. Thanks in advance (2 Replies)
Discussion started by: deepusunil
2 Replies

10. Shell Programming and Scripting

Error Handling -pls advice

Dear friends, I am using the below command in my unix script ----------------------------------------------- File_Name=`ls $CTRY*$DATE_SUFFIX*zip` --> Command-1 ..... if then unzip -a $File_Name -d $CTRY_DIR/$CTRY else echo "File for $CTRY dated $DATE_SUFFIX does not exist... (2 Replies)
Discussion started by: sureshg_sampat
2 Replies
Login or Register to Ask a Question