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

                #----------------------------#
                # Parse any function options.#
                #----------------------------#
                PP_option=0
                PP_opt_l=0
                PP_opt_p=0
                PP_opt_t=0
                PP_pri="$logger_p" # $logger_p and $logger_t are assigned
                PP_tag="$logger_t" # defaults within library defaults.sh.

                PP_root=$tmp/$name_root"_PP_go_"$id_hex
                PP_err=$PP_root"."$Xtimestamp
                while getopts lp:t: PP_opt 2>> $PP_err
                do
                   #-------------------------------------------------------------#
                   # The PP_option and PP_opt_l are necessary to prevent         #
                   # possible command options (which must be specified after     #
                   # function options) from being confused with function options.#
                   #-------------------------------------------------------------#
                   case $PP_opt in
                      l ) PP_opt_l=1
                          PP_option=1
                          ;;
                      p ) PP_opt_p=1
                          PP_pri="$OPTARG"
                          PP_option=1
                          ;;
                      t ) PP_opt_t=1
                          PP_tag="$OPTARG"
                          PP_option=1
                          ;;
                     \? ) echo "$PP_ID" \
                            "Invalid option: -`sed 's/^.*-- //' $PP_err`" 1>&2
                          ;;
                      * ) ;;
                   esac
                done
                [ $PP_option -ne 0 ] && shift `expr $OPTIND - 1` # Shift past options to remaining args
                [ ."$PP_root" != . ] && \rm -f $PP_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). #
                #----------------------------------------------------------------#

                if [ $# -eq 2 ]; then
                   if [ -s $2 ]; then
                      #----------------------------------------------------#
                      # Eval necessary for variables within the parameter. #
                      #----------------------------------------------------#
                      eval echo `$AWK -v pattern="^[\t ]\*$1=" \
                       '/$1[\011 ]*=/
                        {
                          #------------------------------------------------#
                          # The following allows escaped parameter #-signs.#
                          #------------------------------------------------#
                          gsub(/\\\#/,"<OCTOTHORPE>") # Save any '\#'
                          sub(/#+.*/,"")              # Remove comments
                          gsub(/<OCTOTHORPE>/,"\\\#") # Restore any '\#'

                          sub(/ += +/,"=") # Squeeze spaces from around equal-sign
                          if (match($0,pattern))
                          {
                            i = index($0,"=")     # Find position of 1st =-sign
                            if (i > 0)            # and remove everything up to
                              $0 = substr($0,i+1) # and including it.
                            print
                          }
                        }' $2`
                      return 0 # Ahhhh, the sweet smell of success
                   fi
                fi

                #------------------------------------------#
                # Reaching here means something is amiss.  #
                #------------------------------------------#
                if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi

                . $SHLIB/exit.sh
                . $SHLIB/write_err_to_syslogs.sh

                PP_ID="$script_name($PP_name)"
                PP_m2="$script_name terminated."

                if [ $# -eq 2 ]; then
                  if [ ! -f $2 ]; then
                     #-----------------------------------------------------#
                     # Reaching this point means the file is missing or    #
                     # empty.  Stderr is used because EMAIL_MSG will fail  #
                     # without required parameters (like whom to notify).  #
                     #-----------------------------------------------------#
                     PP_subj="FATAL ERROR: $PP_ID"
                     PP_m1="Unable to locate parameter file ($2)."
                     echo "`date '+%Y-%m-%d %T'` $PP_subj" 1>&2
                     echo "${sp}$PP_m1"                    1>&2
                     echo "${sp}$PP_m2"                    1>&2

                     if [ $PP_opt_l -eq 1 -a ."${TERM}" = . ]; then # Logger option?
                        WRITE_ERR_TO_SYSLOGS $syslog_emergency \
                          "ABORT: $PP_subj $PP_m1 $PP_m2"
                     fi
                     EXIT 1
                  fi
                fi

                #--------------------------------------------------------#
                # Reaching this point means we weren't given enough args.#
                #--------------------------------------------------------#
                PP_subj="FATAL ERROR (Function): $PP_ID"
                PP_m1="Insufficient args!"
                echo "`date '+%Y-%m-%d %T'` $PP_subj"                         1>&2
                echo "${sp}$PP_m1"                                            1>&2
                echo "${sp}Args received=$*"                                  1>&2
                echo "${sp}Usage: $PP_name -l -p ppp -t ttt par parfile\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 <ppp>" 1>&2
                echo "${sp}                     (default is 'user.err')."     1>&2
                echo "${sp}                -t = logger tag string <ttt>"      1>&2
                echo "${sp}                     (default is '$LOGNAME[$$]')." 1>&2
                echo "${sp}               par = Parameter string."            1>&2
                echo "${sp}           parfile = Parameter fileid.\n"          1>&2
                echo "${sp}Option notes: Option (-l) MUST precede -p and -t." 1>&2
                echo "${sp}$PP_m2"                                            1>&2

                if [ ."${TERM}" = . ]; then # Logger option?
                   WRITE_ERR_TO_SYSLOGS $syslog_emergency \
                     "ABORT: $PP_subj $PP_m1 $PP_m2"
                fi

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

              #======================================================================#
              #                       D O C U M E N T A T I O N                      #
              #======================================================================#
              #                                                                      #
              #      Author: Bob Orlando                                             #
              #                                                                      #
              #        Date: October 30, 1997                                        #
              #                                                                      #
              #  Program ID: parse_parameters.sh                                     #
              #                                                                      #
              #       Usage: PARSE_PARAMETERS -l -p ppp -t ttt par parfile           #
              #                                                                      #
              #                               -l = Notify via logger command         #
              #                                    (default is false).               #
              #                               -p = logger priority string <ppp>      #
              #                                    (default is "user.err").          #
              #                               -t = logger tag string <ttt>           #
              #                                    (default is "$LOGNAME[$$]").      #
              #                              par = Parameter we seek.                #
              #                          parfile = Contains parameters and values.   #
              #                                                                      #
              #              Option notes: Option (-l) MUST precede -p and -t.       #
              #                                                                      #
              #     Purpose: Extract tunable/variable program parameters from        #
              #              a parameter file.                                       #
              #                                                                      #
              # Description: Library function extracts parameters from a parameter   #
              #              file (parfile), echoing its value to STDOUT. Parameter  #
              #              File format is parameter=value where spaces around the  #
              #              equal sign are ignored.  However value strings (those   #
              #              following the equal sign) cannot contain imbedded       #
              #              blanks.  Parameters may be specified in any order, but  #
              #              they must be in UPPERCASE.  Variable names (e.g. $HOME  #
              #              and $whatever) are allowed since the parameters and     #
              #              their values are passed through an 'eval' command       #
              #              during processing.  The program also allows escaped     #
              #              pound-signs within parameter values (we have not,       #
              #              as yet, found it necessary to escape other special      #
              #              characters).                                            #
              #                                                                      #
              #     Globals: No global variables are assigned in this function.      #
              #              "PP_" prefix identifies local function variables.       #
              #                                                                      #
              # Exit_status: Exits with failure (1) for severe error (e.g. user      #
              #              supplies invalid argument).  This is because inability  #
              #              to parse parameters essential for the program to run    #
              #              "IS" a show stopper.  Otherwise we echo one or more     #
              #              values belonging to the specified parameter (or null    #
              #              if the parameter is unassigned) and return zero.        #
              #                                                                      #
              #       Calls: EXIT and WRITE_ERR_TO_SYSLOGS library function.         #
              #                                                                      #
              #       Notes: ....................................................    #
              #              ....................................................    #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.14 * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.13 * 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