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

                TEN_ID="$script_name:$TEN_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).                             #
                #--------------------------------------------------------------------#
                TEN_options="lptv"
                TEN_opt_l=0
                TEN_opt_p=0
                TEN_opt_t=0
                TEN_opt_v=0
                TEN_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
                TEN_tag="$logger_t" # within library defaults.sh.
                TEN_opts=""
                TEN_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.                                                      #
                   #-------------------------------------------------------------#
                   TEN_n=`expr "$1". : '-.*'`
                   if [ $TEN_n -gt 0 ]; then
                      if [ $TEN_n -eq 2 ]; then
                         TEN_optargs="$1"
                      else
                         TEN_optargs=`echo "$1" | sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
                      fi

                      for TEN_optarg in $TEN_optargs
                      do
                         case $TEN_optarg in
                           -l ) TEN_opt_l=1
                                . $SHLIB/write_err_to_syslogs.sh
                                ;;
                           -p ) if [ $TEN_opt_l -eq 1 -a $TEN_opt_p -eq 0 ]; then
                                   TEN_opt_p=1
                                   if [ -`expr "$2" : '\(\-\)['$TEN_options']'` != "--" ]; then
                                      TEN_pri="$2"
                                      shift
                                   fi
                                else
                                   TEN_opts="$TEN_opts $TEN_optarg"
                                fi
                                ;;
                           -t ) if [ $TEN_opt_l -eq 1 -a $TEN_opt_t -eq 0 ]; then
                                   TEN_opt_t=1
                                   TEN_tag="$2"
                                   if [ -`expr "$2" : '\(\-\)['$TEN_options']'` != "--" ]; then
                                      TEN_tag="$2"
                                      shift
                                   fi
                                else
                                   TEN_opts="$TEN_opts $TEN_optarg"
                                fi
                                ;;
                           -v ) TEN_opt_v=1
                                ;;
                            * ) TEN_args="$TEN_args|$TEN_optarg"
                                ;;
                         esac
                      done # for TEN_optarg in $TEN_optargs
                   else
                      [ ."$TEN_args" != . ]        \
                        && TEN_args="$TEN_args|$1" \
                        || TEN_args="$1"
                   fi
                   shift
                done # while [ ."$1" != . ]

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

                #--------------------------------------------------------------#
                # If we have sufficient args, then attempt our touch command.  #
                #--------------------------------------------------------------#
                TEN_status=0
                TEN_cmd="touch $TEN_opts"

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

                         #-----------------------------------------#
                         # Reaching here means something is amiss. #
                         #-----------------------------------------#
                         TEN_m1="'$TEN_cmd \"$TEN_file\"'"
                         TEN_m2="ERROR=$TEN_stderr"
                         TEN_m3="$script_name terminated."
                         TEN_m4=`ls -al "$TEN_file" | sed "s/^/$sp/"`
                         echo "`date '+%Y-%m-%d %T'` FATAL ERROR: $TEN_ID" 1>&2
                         echo "${sp}$TEN_m1"                               1>&2
                         echo "${sp}$TEN_m2"                               1>&2
                         echo "${sp}$TEN_m3  File list (in any) follows:"  1>&2
                         echo "${sp}$dashes"                               1>&2
                         echo "$TEN_m4"                                    1>&2

                         if [ $TEN_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                            WRITE_ERR_TO_SYSLOGS -p "$TEN_pri" -t "$TEN_tag" \
                              "ABORT: $TEN_ID $TEN_m1 $TEN_m2 $TEN_m3"
                         fi
                         EXIT 1
                      done # while read TEN_file
                   return 0
                fi # if [ ."$TEN_args" != . ]; then

                #--------------------------------------------------------------------#
                # Reaching this point means we have an insufficient arguments error. #
                #--------------------------------------------------------------------#
                TEN_m1="Insufficient args."
                TEN_m2="$script_name terminated."
                echo "`date '+%Y-%m-%d %T'` FATAL ERROR (Function): $TEN_ID"  1>&2
                echo "${sp}$TEN_m1\n"                                         1>&2
                echo "${sp}Usage: $TEN_name -l -p priority -t tag -v"         1>&2
                echo "${sp}                      file [...]\n"                1>&2
                echo "${sp}                 -l = Notify via logger command"   1>&2
                echo "${sp}                      (default is false)"          1>&2
                echo "${sp}                 -p = Logger priority string"      1>&2
                echo "${sp}                      (default is 'user.err')"     1>&2
                echo "${sp}                 -t = Logger tag string"           1>&2
                echo "${sp}                      (default is '$LOGNAME[$$]')" 1>&2
                echo "${sp}                 -v = Verbose\n"                   1>&2
                echo "${sp}Option notes: Option (-l) MUST precede -p and -t." 1>&2
                echo "${sp}$TEN_m2"                                           1>&2
                if [ $TEN_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                   WRITE_ERR_TO_SYSLOGS -p "$TEN_pri" -t "$TEN_tag" \
                     "ABORT: $TEN_ID $TEN_m1 $TEN_m2"
                fi

                EXIT 1
              } # "TEN_" 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_NOMAIL -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       #
              #              stderr 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.  Unlike TOUCH_EXIT_ERR,        #
              #              notification here is via echo to stderr (not via        #
              #              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.        #
              #              "TEN_" prefix identifies local function variables.      #
              #                                                                      #
              #       Calls: EXIT library function.  (DOES NOT use EMAIL_MSG         #
              #              library function for error reporting/notification.)     #
              #              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: .....................................................   #
              #              .....................................................   #
              #                                                                      #
              #    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