# SccsId[] = "%W% (USL function) %G%"
              E_name="EXIT"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set|egrep '^$E_name\(\)\{$'`" != . ] && E_loaded=1
              else # Korn or Bash shell and function already loaded?
                 if [ `expr "\`uname -s\`" : "[Ll][Ii][Nn][Uu][Xx]"` -eq 0 ]; then
                    [ ."`typeset +f|awk '/^'$E_name'[=\(]?/'`" != . ] && E_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$E_name'[=\(]?/'`" != . ] && E_loaded=1
                 fi
              fi
              if [ 0${E_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              EXIT() # Function documentation located at bottom.                     #
              #----------------------------------------------------------------------#
              { [ ."${AWK}" = . ] && { { [ -x /usr/bin/nawk ] && AWK=/usr/bin/nawk; } \
                                  ||   { [ -x /bin/gawk     ] && AWK=/bin/gawk    ; } \
                                  ||   { [ -x /usr/bin/awk  ] && AWK=/usr/bin/awk ; }; }

                if [ $# -eq 0 ]; then   # If no argument,
                   E_exit_status=$?     # then use $?.
                elif [ $# -eq 1 ]; then # If we have an argument, validate it.
                   $AWK 'BEGIN {
                                 if (ARGV[1] ~ /^[0-9]+$/)
                                   exit (int(ARGV[1]) <= 255) ? 0 : 1
                                 else
                                   exit 255
                               }' $1
                   E_exit_status=$? # Cannot exceed 255 for Unix
                fi

                if [ $E_exit_status -eq 0 ]; then # If status is a valid digit,
                   E_exit_status=$1               # assign it to E_exit_status.
                else
                   #-----------------------------------------#
                   # Reaching here means something is amiss. #
                   #-----------------------------------------#
                   if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi

                   . $SHLIB/email_msg.sh            # Calls $SHLIB/exit.sh
                   . $SHLIB/write_err_to_syslogs.sh

                   #------------------------------------------------------------#
                   # If the following variables are not set, use these defaults.#
                   #------------------------------------------------------------#
                   : ${script_name:=`basename $0`}
                   : ${sp:="                    "}
                   E_ID="$script_name($E_name)"

                   if [ $E_exit_status -eq 1 ]; then
                      E_m1="Non-numeric exit status ($1)."
                      E_m2="Exiting with status=1."
                      EMAIL_MSG "WARNING $E_ID" \
                        "${sp}$E_m1"            \
                        "${sp}$E_m2"
                      WRITE_ERR_TO_SYSLOGS $syslog_warn "$EAR_ID $E_subj $E_m1 $E_m2"
                      # Don't exit here
                   elif [ $E_exit_status -eq 255 ]; then
                      E_exit_status=255
                      E_m1="UNIX exit status must be an integer between 0-255"
                      E_m1="$E_m1 (you supplied $1)."
                      E_m2="Exiting with status=$E_exit_status."
                      EMAIL_MSG "WARNING $E_ID" \
                        "${sp}$E_m1"            \
                        "${sp}$E_m2."
                      WRITE_ERR_TO_SYSLOGS $syslog_warn "$EAR_ID $E_subj $E_m1 $E_m2"
                      # Don't exit here either
                   fi
                fi

              # if [ ."$stderr" != . ]; then          # 20040823.080910
              #    [ -s $stderr ] && cat $stderr 1>&2 # This is just a thought.
              # fi                                    # Don't know that I will
              # if [ ."$stdout" != . ]; then          # (or should) implement this.
              #    [ -s $stdout ] && cat $stdout
              # fi

                if [ .$active_status_file != . -a -f "$active_status_file" ]; then
                   [ 0`cat $active_status_file` -eq $$ ] && \rm -f $active_status_file
                fi
                exit $E_exit_status
              } # "E_" prefix identifies this function's local variables.
              fi

              #======================================================================#
              #                       D O C U M E N T A T I O N                      #
              #======================================================================#
              #                                                                      #
              #      Author: Bob Orlando                                             #
              #                                                                      #
              #        Date: December 14, 1996                                       #
              #                                                                      #
              #  Program ID: exit.sh                                                 #
              #              (The ".sh" distinguishes it from "exit" command.)       #
              #                                                                      #
              #       Usage: EXIT [optional exit status (0-255)                      #
              #              (Uppercased EXIT distinguishes it from the command.)    #
              #                                                                      #
              #     Purpose: Exit with an (optional) exit status (validating it)     #
              #              and with some basic housekeeping.                       #
              #                                                                      #
              #     Globals: Tests for presence of $active_status_file.              #
              #              "E_" prefix identifies local function variables.        #
              #                                                                      #
              # Exit_status: Warns of function error (e.g. user supplies invalid     #
              #              argument).  Otherwise exits with either the default     #
              #              status or the status passed to us in $1.                #
              #                                                                      #
              #       Calls: EMAIL_MSG library function.                             #
              #                                                                      #
              #       Notes: Housekeeping consists of testing for the existance      #
              #              of the variable and file, $active_status_file.  If      #
              #              it exists, then it is removed before exiting.           #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.10 * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.9  * Change set|egrep|awk to just set|egrep.      #
              #                                                                      #
              #----------------------------------------------------------------------#
            
Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1996-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando