Home » Articles » Shell Script function to manage execution error

Shell Script function to manage execution error

A simple shell script to manage execution error and display a meaningful message to the user

This function was designed for Korn Shell *ksh93* and should be POSIX Compliant. Please message me if you have suggestion to make it better.

### Simple function to display separator character on the size of terminal width
f_print_separator () #noinput
{
 for i in {1..79};do printf '*'; done
 printf '%s\n' "*"
}

### Function that display meaningful error messages when error occur
f_error () #$1=errortypei & errornum & message
{
    # [[ ! -z "$debug" ]] && set -x

    ### Feel free to add your own custom error message 
    ### Don't forget to add a trap in next section for your custom error number

    ### Logic is 
    ###  - All error that are received by trap need to exit inside the function 
    ###    (case area) to simulate the normal behavior of an exiting
    ###  - All error that are called directly in the code must do an exit after
    ###    the function call. In order to have the 
    ###    flexibility to create specific step before exiting the script.

     
    dtg=`date +%D\ %H:%M:%S`
    if [[ ! "$1" = "" ]];then
        errortype=$1; shift
        errornum=$1; shift
        errormsg=$1; shift
    fi

    echo ""
    f_print_separator
    echo ""
    echo "$errortype: $errornum"
    echo ""

    ert="$dtg: $scriptname: $errortype"
    case $errornum in
      000) erm="${ert}: Normal Termination ${errormsg}" ;;
      001) erm="${ert}: Terminated by signal HUP; ${errormsg}";
        [[ ! -z tmpfile ]] && rm -f ${tmpfile}
        echo "$erm"
        kill -HUP $PID ;;
      002) erm="${ert}: Terminated by signal INT;${errormsg}";
        [[ ! -z tmpfile ]] && rm -f ${tmpfile}
        echo "$erm"
        kill -INT $PID ;;
      003) erm="${ert}: Terminated by signal QUIT; ${errormsg}";
        [[ ! -z tmpfile ]] && rm -f ${tmpfile}
        echo "$erm"
        kill -QUIT $PID ;;
      015) erm="${ert}: Terminated by signal TERM; ${errormsg}";
        [[ ! -z tmpfile ]] && rm -f ${tmpfile}
        echo "$erm"
        kill -TERM $PID ;;
      123) erm="${ert}: Non Fatal Error; ${errormsg}" ;;
      124) erm="${ert}: No Parameter found; ${errormsg}" ;;
      125) erm="${ert}: Invalid command line flag. ${errormsg}" ;;
      126) erm="${ert}: File or Directory doesn't exist! $errormsg" ;;
      127) erm="${ert}: Script exiting!";
        trap '-' EXIT
        [[ ! -z tmpfile ]] && rm -f ${tmpfile}
        echo "$erm"
        kill -TERM $PID ;;
      *) erm="${ert}: Unallocated error ............$errornum; ${errormsg}" ;;
    esac
    echo ""
    echo "$erm"
    echo ""
    f_print_separator
    echo ""
    echo ""

    
    ######## Example of usage of error function
    ### 1. This example should be included in all script at the very end
    ###    to show a normal run exit message. (at end of script after normal execution)
    ###    f_error INFO 000 "The script ran successfully" 
    ###
    ### 2. The following example is for ERROR type that doesn't exit in the f_error itself.
    ###    f_error ERROR 124 "The script encountered an error and must terminate." 
    ###        display_usage
    ###        exit 124
    ###
    ### 3. The following examples are for the ERROR type that exit in the f_error function itself
    ###        trap 'f_error ERROR 001' "HUP"
    ###    trap 'f_error ERROR 002' "INT"
    ###    trap 'f_error ERROR 003' "QUIT"
    ###        trap 'f_error ERROR 015' "TERM"
    ###
    ### Make sure when trapping exiting signal to kill the process also or the process will hang
    ### [[ ! -z tmpfile ]] && rm -f ${tmpfile}
    ### kill -[SIG_TRAPPED] $PID;;
    ### 
    ########
}

Leave a Reply