# SccsId[] = "%W% (USL function) %G%"
              TE_name="TOUCH_EXIT_ERR"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set|egrep '^$TE_name\(\)\{$'`" != . ] && TE_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 '/^'$TE_name'[=\(]?/'`" != . ] && TE_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$TE_name'[=\(]?/'`" != . ] && TE_loaded=1
                 fi
              fi
              if [ 0${TE_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              TOUCH_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

                TE_ID="$script_name($TE_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).                             #
                #--------------------------------------------------------------------#
                TE_options="lptv"
                TE_opt_l=0
                TE_opt_p=0
                TE_opt_t=0
                TE_opt_v=0
                TE_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
                TE_tag="$logger_t" # within library defaults.sh.
                TE_opts=""
                TE_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.                                                      #
                   #-------------------------------------------------------------#
                   TE_n=`expr "$1". : '-.*'`
                   if [ $TE_n -gt 0 ]; then
                      if [ $TE_n -eq 2 ]; then
                         TE_optargs="$1"
                      else
                         TE_optargs=`echo "$1" | sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
                      fi

                      for TE_optarg in $TE_optargs
                      do
                         case $TE_optarg in
                           -l ) TE_opt_l=1
                                . $SHLIB/write_err_to_syslogs.sh
                                ;;
                           -p ) if [ $TE_opt_l -eq 1 -a $TE_opt_p -eq 0 ]; then
                                   TE_opt_p=1
                                   if [ -`expr "$2" : '\(\-\)['$TE_options']'` != "--" ]; then
                                      TE_pri="$2"
                                      shift
                                   fi
                                else
                                   TE_opts="$TE_opts $TE_optarg"
                                fi
                                ;;
                           -t ) if [ $TE_opt_l -eq 1 -a $TE_opt_t -eq 0 ]; then
                                   TE_opt_t=1
                                   TE_tag="$2"
                                   if [ -`expr "$2" : '\(\-\)['$TE_options']'` != "--" ]; then
                                      TE_tag="$2"
                                      shift
                                   fi
                                else
                                   TE_opts="$TE_opts $TE_optarg"
                                fi
                                ;;
                           -v ) TE_opt_v=1
                                ;;
                            * ) TE_args="$TE_args|$TE_optarg"
                                ;;
                         esac
                      done # for TE_optarg in $TE_optargs
                   else
                      [ ."$TE_args" != . ]       \
                        && TE_args="$TE_args|$1" \
                        || TE_args="$1"
                   fi
                   shift
                done # while [ ."$1" != . ]

                TE_opts=`expr "$TE_opts" : ' *\(.*\)'` # Strip leading blanks
                TE_args=`expr "$TE_args" : '|*\(.*\)'` # Strip leading stiles

                #-----------------------------------------------------------------#
                # If we have sufficient args, then attempt our touch command.     #
                #-----------------------------------------------------------------#
                TE_status=0
                TE_cmd="touch $TE_opts"

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

                         #-----------------------------------------#
                         # Reaching here means something is amiss. #
                         #-----------------------------------------#
                         TE_m1="'$TE_cmd \"$TE_file\"'"
                         TE_m2="ERROR=$TE_stderr"
                         TE_m3="$script_name terminated."
                         TE_m4=`ls -al "$TE_file" | sed "s/^/$sp/"`
                         EMAIL_MSG "FATAL ERROR: $TE_ID"              \
                           "${sp}$TE_m1"                              \
                           "${sp}$TE_m2"                              \
                           "${sp}$TE_m3  File list (in any) follows:" \
                           "${sp}$dashes"                             \
                           "$TE_m4"

                         if [ $TE_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                            WRITE_ERR_TO_SYSLOGS -p "$TE_pri" -t "$TE_tag" \
                              "ABORT: $TE_ID $TE_m1 $TE_m2 $TE_m3"
                         fi
                         EXIT 1
                      done # while read TE_file
                   return 0
                fi # if [ ."$TE_args" != . ]; then

                #--------------------------------------------------------------------#
                # Reaching this point means we have an insufficient arguments error. #
                #--------------------------------------------------------------------#
                TE_m1="Insufficient args."
                TE_m2="$script_name terminated."
                EMAIL_MSG "FATAL ERROR (Function): $TE_ID"                 \
                  "${sp}$TE_m1\n"                                          \
                  "${sp}Usage: $TE_name -l -p priority -t tag -v"          \
                  "${sp}                     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}$TE_m2"
                if [ $TE_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                   WRITE_ERR_TO_SYSLOGS -p "$TE_pri" -t "$TE_tag" \
                     "ABORT: $TE_ID $TE_m1 $TE_m2"
                fi

                EXIT 1
              } # "TE_" 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: December 14, 1996                                       #
              #                                                                      #
              #  Program ID: touch_exit_err.sh                                       #
              #                                                                      #
              #       Usage: TOUCH_EXIT_ERR -l -p priority -t tag -v 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: Touch one or more files, reporting any errors via       #
              #              email and logger, and exiting on error.                 #
              #                                                                      #
              # Description: This function adds no new functionality to the UNIX     #
              #              touch command beyond automatic notification/logging     #
              #              before exiting on error, and isolating the specific     #
              #              file being touched in case of error.  Notification is   #
              #              provided primarily 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 be passed (both have defaults if omitted).       #
              #                                                                      #
              #              In an effort to handle really dumb filenames (those     #
              #              with embedded blanks), no touch-specific options can    #
              #              be passed to this script.  Any options so passed        #
              #              (other than the ones listed above) are treated as       #
              #              target files to be touched.                             #
              #                                                                      #
              #     Globals: No global variables assigned from this function.        #
              #              "TE_" prefix identifies local function variables.       #
              #                                                                      #
              #       Calls: EMAIL_MSG and EXIT library functions.                   #
              #              Optionally, /usr/bin/logger is called.                  #
              #                                                                      #
              # Exit_status: Exits with failure (1) on error.  This is because       #
              #              any error while touching a file is potentially a        #
              #              show stopper.  Else, returns 0.                         #
              #                                                                      #
              #       Notes: DO NOT use this routine to touch $log or $mailmsg.      #
              #              Use TOUCH_EXIT_ERR_NOMAIL instead.  This is because     #
              #              this function calls EMAIL_MSG function (a function      #
              #              which uses both $log and $mailmsg to report problems).  #
              #              Any problems changing $log and $mailmsg, therefore,     #
              #              cannot be reported by EMAIL_MSG using mail.             #
              #                                                                      #
              #    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, 1996-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando