# SccsId[] = "%W% (USL function) %G%"
              RM_name="RM_EXIT_ERR"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set|egrep '^$RM_name\(\)\{$'`" != . ] && RM_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 '/^'$RM_name'[=\(]?/'`" != . ] && RM_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$RM_name'[=\(]?/'`" != . ] && RM_loaded=1
                 fi
              fi
              if [ 0${RM_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              RM_EXIT_ERR() # 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 [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi

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

                #------------------------------------------------------------#
                # If the following variables are not set, use these defaults.#
                #------------------------------------------------------------#
                : ${script_name:=`basename $0`}
                : ${name_root:=`echo $script_name|$AWK '{sub(/^\.+/,"");sub(/\..*/,"");print}'`}
                : ${logger_p="user.err"}
                : ${logger_t="$LOGNAME[$$]"}
                : ${verbose:=0}
                : ${sp:="                    "}
                : ${dashes:="------------------------------------------------------"}
                : ${tmp:=/var/tmp}

                if [ ."$log" = . ]; then
                   log=$name_root.log
                   [ ."$teelog" = . ] && teelog="cat"
                fi

                RM_ID="$script_name($RM_name)"

                #--------------------------------------------------------------------#
                #               Process function options and arguments               #
                #               --------------------------------------               #
                # We use while-loop instead of getopts because -p (logger priority)  #
                # option and -t (logger tag option) may or may not have an optarg    #
                # (the user may just want the defaults).                             #
                #--------------------------------------------------------------------#
                RM_options="lptvfR"
                RM_opt_l=0
                RM_opt_p=0
                RM_opt_t=0
                RM_opt_v=0
                RM_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
                RM_tag="$logger_t" # within library defaults.sh.
                RM_opts="-f"
                RM_args=""

                while [ ."$1" != . ]
                do
                   #-------------------------------------------------------------#
                   # If we have an option list (e.g. "-xyz") change that list to #
                   # individual "-x -y -z" options.  Else, just pass the option  #
                   # along.                                                      #
                   #-------------------------------------------------------------#
                   RM_n=`expr "$1". : '-.*'`
                   if [ $RM_n -gt 0 ]; then
                      if [ $RM_n -eq 2 ]; then
                         RM_optargs="$1"
                      else
                         RM_optargs=`echo "$1"|sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
                      fi

                      for RM_optarg in $RM_optargs
                      do
                         case $RM_optarg in
                           -l ) RM_opt_l=1
                                . $SHLIB/write_err_to_syslogs.sh
                                ;;
                           -p ) if [ $RM_opt_l -eq 1 -a $RM_opt_p -eq 0 ]; then
                                   RM_opt_p=1
                                   if [ -`expr "$2" : '\(\-\)['$RM_options']'` != "--" ]; then
                                      RM_pri="$2"
                                      shift
                                   fi
                                else
                                   RM_opts="$RM_opts $RM_optarg"
                                fi
                                ;;
                           -t ) if [ $RM_opt_l -eq 1 -a $RM_opt_t -eq 0 ]; then
                                   RM_opt_t=1
                                   RM_tag="$2"
                                   if [ -`expr "$2" : '\(\-\)['$RM_options']'` != "--" ]; then
                                      RM_tag="$2"
                                      shift
                                   fi
                                else
                                   RM_opts="$RM_opts $RM_optarg"
                                fi
                                ;;
                           -v ) RM_opt_v=1
                                ;;
                           -f ) # This is an 'rm' option we already use--skip this one.
                                ;;
                           -R ) # This is an 'rm' option--just pass it along.
                                RM_opts="$RM_opts $RM_optarg"
                                ;;
                         esac
                      done # for RM_optarg in $RM_optargs
                   else
                      [ ."$RM_args" != . ]       \
                        && RM_args="$RM_args|$1" \
                        || RM_args="$1"
                   fi
                   shift
                done # while [ ."$1" != . ]
                RM_opts=`expr "$RM_opts" : ' *\(.*\)'` # Strip leading blanks
                RM_args=`expr "$RM_args" : '|*\(.*\)'` # Strip leading stiles

                #--------------------------------------------------------------#
                # If we have sufficient args, then attempt our remove command. #
                #--------------------------------------------------------------#
                RM_status=0
                RM_cmd="rm $RM_opts"

                if [ ."$RM_args" != . ]; then
                   echo "$RM_args"|awk -F\| '{for (i=1;i<=NF;i++) print $i}' \
                    | while read RM_file
                      do
                         [ $verbose -eq 1 -o $RM_opt_v -eq 1 ] \
                           && echo "`date '+%Y-%m-%d %T'` $RM_cmd \"$RM_file\"" \
                             | $teelog
                         RM_stderr=`$RM_cmd "$RM_file" 2>&1`
                         RM_status=$?
                         [ $RM_status -eq 0 ] && continue

                         #-----------------------------------------#
                         # Reaching here means something is amiss. #
                         #-----------------------------------------#
                         RM_m1="'$RM_cmd \"$RM_file\"'"
                         RM_m2="ERROR=$RM_stderr"
                         RM_m3="$script_name terminated."
                         RM_m4=`ls -al "$RM_file"|sed "s/^/$sp/"`
                         EMAIL_MSG "FATAL ERROR: $RM_ID"              \
                           "${sp}$RM_m1"                              \
                           "${sp}$RM_m2"                              \
                           "${sp}$RM_m3  File list (in any) follows:" \
                           "${sp}$dashes"                             \
                           "$RM_m4"

                         if [ $RM_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                            WRITE_ERR_TO_SYSLOGS -p "$RM_pri" -t "$RM_tag" \
                              "ABORT: $RM_ID $RM_m1 $RM_m2 $RM_m3"
                         fi
                         EXIT 1
                      done # while read RM_file
                   return 0
                fi # if [ ."$RM_args" != . ]; then

                #--------------------------------------------------------------------#
                # Reaching this point means we have an insufficient arguments error. #
                #--------------------------------------------------------------------#
                RM_m1="Insufficient args."
                RM_m2="$script_name terminated."
                EMAIL_MSG "FATAL ERROR (Function): $RM_ID"                 \
                  "${sp}$RM_m1\n"                                          \
                  "${sp}Usage: $RM_name -l -p priority -t tag -v"          \
                  "${sp}                     [rm-options] file ...\n"      \
                  "${sp}                -l = Notify via logger command"    \
                  "${sp}                     (default is false)"           \
                  "${sp}                -p = Logger priority string"       \
                  "${sp}                     (default is 'user.err')"      \
                  "${sp}                -t = Logger tag string"            \
                  "${sp}                     (default is '$LOGNAME[$$]')"  \
                  "${sp}                -v = Verbose\n"                    \
                  "${sp}Option notes: Option (-l) MUST precede -p and -t." \
                  "${sp}              'RM-options' MUST follow function"   \
                  "${sp}               options."                           \
                  "${sp}$RM_m2"
                if [ $RM_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                   WRITE_ERR_TO_SYSLOGS -p "$RM_pri" -t "$RM_tag" \
                     "ABORT: $RM_ID $RM_m1 $RM_m2"
                fi

                EXIT 1
              } # "RM_" prefix identifies this function's local variables.
              fi

              #======================================================================#
              #                       D O C U M E N T A T I O N                      #
              #======================================================================#
              #                                                                      #
              #      Author: Bob Orlando (Bob@OrlandoKuntao.com)                     #
              #                                                                      #
              #        Date: April 24, 1997                                          #
              #                                                                      #
              #  Program ID: rm_exit_err.sh                                          #
              #                                                                      #
              #       Usage: RM_EXIT_ERR -l -p priority -t tag -v \                  #
              #                               [rm-options] file ...                  #
              #                                                                      #
              #                          -l = Notify via logger command              #
              #                               (default is false)                     #
              #                          -p = Logger priority string                 #
              #                               (default is "user.err")                #
              #                          -t = Logger tag string                      #
              #                               (default is "$LOGNAME[$$]")            #
              #                          -v = Verbose                                #
              #                                                                      #
              #              Option notes: Option (-l) MUST precede other options.   #
              #                            'UNIX command options' MUST follow        #
              #                            function options.  If command option      #
              #                            is -l, -p, or -t, and you've specified    #
              #                            function option -l, then you must also    #
              #                            supply both function options, -p and -t.  #
              #                                                                      #
              #     Purpose: Remove files with error checking and reporting          #
              #              via email and logger, and exiting on error.             #
              #                                                                      #
              # Description: This function adds no new functionality to the UNIX     #
              #              rm command beyond automatic notification/logging        #
              #              before exiting on error.  Notification is provided      #
              #              via the EMAIL_MSG library function.  Optionally         #
              #              (-l), the user may have the error logged in syslogs     #
              #              (/var/adm/messages) via logger command.  Logger -p      #
              #              and -t options and option arguments also may be         #
              #              passed (both have defaults if omitted).                 #
              #                                                                      #
              #     Globals: No global variables assigned from this function.        #
              #              "RM_" prefix identifies local function variables.       #
              #                                                                      #
              #       Calls: EMAIL_MSG and EXIT library functions.                   #
              #              Optionally, WRITE_ERR_TO_SYSLOGS is also called.        #
              #                                                                      #
              # Exit_status: Exits with failure (1) on error.  This is because       #
              #              any error while removing a file is potentially a        #
              #              show stopper.  Else returns 0.                          #
              #                                                                      #
              #     Notes:   If the user wishes NOT to exit on error, then he        #
              #              should use rm's force (-f) flag.  For example,          #
              #              RM_EXIT_ERR -f anyfile.  Regardless of existence        #
              #              or file permissions, the rm returns a status of 0       #
              #              (success).                                              #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.16 * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.15 * Change set|egrep|awk to just set|egrep.      #
              #                                                                      #
              #----------------------------------------------------------------------#
            
Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1997-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando