# SccsId[] = "%W% (USL function) %G%"
              WES_name="WRITE_ERR_TO_SYSLOGS"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set | egrep '^$WES_name\(\)\{$'`" != . ] && WES_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 '/^'$WES_name'[=\(]?/'`" != . ] && WES_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$WES_name'[=\(]?/'`" != . ] && WES_loaded=1
                 fi
              fi
              if [ 0${WES_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              WRITE_ERR_TO_SYSLOGS() # 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 the following variables are not set, use these defaults.#
                #------------------------------------------------------------#
                : ${id_num=`id|sed 's/^\(uid=\)\([0-9]*\)\(.*\)/\2/'`}
                : ${id_hex=`echo "obase=16;$id_num"|bc`}
                : ${script_name:=`basename $0`}
                : ${name_root:=`echo $script_name|$AWK '{sub(/^\.+/,"");sub(/\..*/,"");print}'`}
                : ${yymmddhhmiss:=`date '+%y''%m%d%H''%M''%S'`}
                : ${Xtimestamp:=`echo "obase=16;$yymmddhhmiss+$$"|bc`}
                : ${sp:="                    "}
                : ${stdout_sp:="             STDOUT="}
                : ${stderr_sp:="             STDERR="}
                : ${tmp:=/var/tmp}
                : ${stdout_root:=$tmp/$name_root"_s1_"$id_hex"."}
                : ${stdout:=${stdout_root}$Xtimestamp}
                : ${stderr_root:=$tmp/$name_root"_s2_"$id_hex"."}
                : ${stderr:=${stderr_root}$Xtimestamp}

                WES_ID="$script_name($WES_name)"

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

                #--------------------------#
                # Parse function's options.#
                #--------------------------#
                WES_option=0
                WES_nomail=0 # No mail (via call to EMAIL_MSG)
                WES_opt_p=0
                WES_opt_t=0
                WES_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
                WES_tag="$logger_t" # within library defaults.sh.

                WES_root=$tmp/$name_root"_WES_go_"$id_hex
                WES_err=$WES_root"."$Xtimestamp
                while getopts np:t: WES_opt 2>> $WES_err
                do
                   case $WES_opt in
                      n ) WES_nomail=1
                          WES_option=1
                          ;;
                      p ) WES_opt_p=1
                          WES_pri="$OPTARG"
                          WES_option=1
                          ;;
                      t ) WES_opt_t=1
                          WES_tag="$OPTARG"
                          WES_option=1
                          ;;
                     \? ) echo "$WES_ID" \
                            "Invalid option: -`sed 's/^.*-- //' $WES_err`" 1>&2
                          ;;
                      * ) ;;
                   esac
                done
                #-------------------------------------------------------------#
                # Shift to remaining arguments (don't you hare arguments).    #
                #-------------------------------------------------------------#
                [ $WES_option -ne 0 ] && shift `expr $OPTIND - 1`
                [ ."$WES_root" != . ] && \rm -f $WES_root* > /dev/null 2>&1

                #----------------------------------------------------------------#
                # Must reset this dog if this function is apt to be called again #
                OPTIND=1  # (try and find this fact documented anywhere else).   #
                #----------------------------------------------------------------#

                #----------------------------------------------------#
                # Reaching this point means that something is amiss. #
                #----------------------------------------------------#
                if [ $WES_nomail -eq 0 ]; then
                   if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi
                   . $SHLIB/email_msg.sh # Calls $SHLIB/exit.sh
                fi

                if [ $# -gt 0 ]; then
                   echo "`date '+%Y-%m-%d %T'`" \
                     "logger -p $WES_pri -t  $WES_tag   $@" | $teelog
                      logger -p $WES_pri -t "$WES_tag" "$@" > $stdout 2> $stderr
                   WES_status=$?

                   [ -s $stderr ] && sed "s/^/$stderr_sp/" $stderr | $teelog
                   if [ $WES_status -eq 0 ]; then
                      [ -s $stdout ] && sed "s/^/$sp/"     $stdout | $teelog
                      return 0
                   fi

                   [ -s $stdout ] && sed "s/^/$stdout_sp/" $stdout | $teelog
                   WES_m1="ERROR: $WES_ID"
                   WES_m2="${sp}Logger command completed with nonzero"
                   WES_m2="$WES_m2 ($WES_status) status."
                   if [ $WES_nomail -eq 0 ]; then
                      EMAIL_MSG "$WES_m1" \
                                "$WES_m2"
                   else
                      echo "`date '+%Y-%m-%d %T'` $WES_m1" 1>&2
                      echo                       "$WES_m2" 1>&2
                   fi
                   return $WES_status
                fi

                #--------------------------------------------------------------------#
                # Reaching this point with zero status means insufficient args error.#
                #--------------------------------------------------------------------#
                WES_usage=`$AWK -v sp="$sp"                   \
                                -v WES_ID="$WES_ID"           \
                                -v WES_name="$WES_name"       \
                                -v script_name="$script_name" \
                  'BEGIN \
                   {
                     print "ERROR: "WES_ID                                       ,
                       "\n"sp"Usage: "WES_name" [-n] [-p \047priority\047] [-t \047tag\047] \134",
                       "\n"sp"              \047Error message string\047\n"      ,
                       "\n"sp"              -n = No mail.  Do not use library"   ,
                       "\n"sp"                   function, EMAIL_MSG,"           ,
                       "\n"sp"                   for reporting function errors." ,
                       "\n"sp"                   Only echo errors (for logging"  ,
                       "\n"sp"                   purposes)."                     ,
                       "\n"sp"              -p = logger priority string"         ,
                       "\n"sp"                   (default is 'user.err')"        ,
                       "\n"sp"              -t = logger tag string"              ,
                       "\n"sp"                   (default is '$LOGNAME[$$]')\n"  ,
                       "\n"sp"Option notes: Options and args MUST be specified " ,
                       "\n"sp"              Priority and tag strings must quoted",
                       "\n"sp"              if they contain imbedded blanks."
                   }'`
                if [ $WES_nomail -eq 0 ]; then
                   EMAIL_MSG "$WES_usage"
                else
                   echo "`date '+%Y-%m-%d %T'` $WES_usage"  1>&2
                fi
                return 1
              } # "WES_" 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: July 27, 1999                                           #
              #                                                                      #
              #  Program ID: write_err_to_syslogs.sh                                 #
              #                                                                      #
              #       Usage: WRITE_ERR_TO_SYSLOGS [-n] [-p priority] [-t tag]  \     #
              #                                   "Error message string"             #
              #                                                                      #
              #                          -n = No mail.  Do not use the library       #
              #                               function, EMAIL_MSG, for reporting     #
              #                               function errors.  Only echo the        #
              #                               errors (for logging purposes).         #
              #                          -p = Logger priority string                 #
              #                               (defaults to "user.err" --             #
              #                                see defaults.sh)                      #
              #                          -t = Logger tag string                      #
              #                               (defaults to "$LOGNAME[$$]" --         #
              #                                see defaults.sh)                      #
              #                                                                      #
              #              Option notes: Priority string must NOT be quoted and    #
              #                            it must conform to one of the classes     #
              #                            found in /etc/syslog.conf.  The tag       #
              #                            string, however, must be quoted if it     #
              #                            contains imbedded blanks.                 #
              #                                                                      #
              #     Purpose: Write error message to syslogs (var/adm/messages)       #
              #              returning logger's exit status.                         #
              #                                                                      #
              # Description: This function adds no new functionality to the UNIX     #
              #              logger command beyond validation of options and         #
              #              automatic notification on error.  Notification is       #
              #              provided via the EMAIL_MSG library function.            #
              #                                                                      #
              #     Globals: No global variables assigned from this function.        #
              #              "WES_" prefix identifies local function variables.      #
              #                                                                      #
              #       Calls: EMAIL_MSG library function (unless nomail option        #
              #              given) and /bin/logger.                                 #
              #                                                                      #
              # Exit Status: Non-zero on error.  Else, returns zero (success).       #
              #                                                                      #
              #       Notes: .....................................................   #
              #              .....................................................   #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.13 * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.12 * Change set|egrep|awk to just set|egrep.      #
              #                                                                      #
              #----------------------------------------------------------------------#
            
Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1999-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando