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

                CMN_ID="$script_name($CMN_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).                             #
                #--------------------------------------------------------------------#
                CMN_options="lptv"
                CMN_opt_l=0
                CMN_opt_p=0
                CMN_opt_t=0
                CMN_opt_v=0
                CMN_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
                CMN_tag="$logger_t" # within library defaults.sh.
                CMN_opts=""
                CMN_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.                                                      #
                   #-------------------------------------------------------------#
                   CMN_n=`expr "$1". : '-.*'`
                   if [ $CMN_n -gt 0 ]; then
                      if [ $CMN_n -eq 2 ]; then
                         CMN_optargs="$1"
                      else
                         CMN_optargs=`echo "$1"|sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
                      fi

                      for CMN_optarg in $CMN_optargs
                      do
                         case $CMN_optarg in
                           -l ) CMN_opt_l=1
                                . $SHLIB/write_err_to_syslogs.sh
                                ;;
                           -p ) if [ $CMN_opt_l -eq 1 -a $CMN_opt_p -eq 0 ]; then
                                   CMN_opt_p=1
                                   if [ -`expr "$2" : '\(\-\)['$CMN_options']'` != "--" ]; then
                                      CMN_pri="$2"
                                      shift
                                   fi
                                else
                                   CMN_opts="$CMN_opts $CMN_optarg"
                                fi
                                ;;
                           -t ) if [ $CMN_opt_l -eq 1 -a $CMN_opt_t -eq 0 ]; then
                                   CMN_opt_t=1
                                   CMN_tag="$2"
                                   if [ -`expr "$2" : '\(\-\)['$CMN_options']'` != "--" ]; then
                                      CMN_tag="$2"
                                      shift
                                   fi
                                else
                                   CMN_opts="$CMN_opts $CMN_optarg"
                                fi
                                ;;
                           -v ) CMN_opt_v=1
                                ;;
                         esac
                      done # for CMN_optarg in $CMN_optargs
                   else
                      [ ."$CMN_args" != . ]        \
                        && CMN_args="$CMN_args|$1" \
                        || CMN_args="$1"
                   fi
                   shift
                done # while [ ."$1" != . ]
                CMN_opts=`expr "$CMN_opts" : ' *\(.*\)'` # Strip leading blanks
                CMN_args=`expr "$CMN_args" : '|*\(.*\)'` # Strip leading stiles

                #----------------------------------------------------------#
                # If we still have args, then the first MUST be the perms. #
                #----------------------------------------------------------#
                if [ ."$CMN_args" != . ]; then
                   CMN_perms=""
                   echo "$CMN_args"|awk -F\| '{for (i=1;i<=NF;i++) print $i}' \
                    | while read CMN_file
                      do
                         if [ ."$CMN_perms" = . ]; then
                            CMN_perms="$CMN_file" # 1st arg MUST be the perms.
                            CMN_cmd="chmod $CMN_opts $CMN_perms"
                            continue              # Got it, so iterate our for-loop.
                         fi

                         CMN_status=0
                         if [ ! \( -f "$CMN_file" -o -d "$CMN_file" \) ]; then
                            CMN_m1="Skipping $CMN_cmd '$CMN_file'."
                            CMN_m2="'$CMN_file' not found!"
                            CMN_m3="$script_name continues."
                            echo "`date '+%Y-%m-%d %T'` WARNING: $CMN_ID" 1>&2
                            echo "${sp}$CMN_m1"                           1>&2
                            echo "${sp}$CMN_m2"                           1>&2
                            echo "${sp}$CMN_m3"                           1>&2
                         elif [ -w "$CMN_file" ]; then # If file is writable, then do ...
                            [ $verbose -eq 1 -o $CMN_opt_v -eq 1 ] \
                              && echo "`date '+%Y-%m-%d %T'` $CMN_cmd \"$CMN_file\"" \
                                | $teelog
                            CMN_stderr=`$CMN_cmd "$CMN_file" 2>&1`
                            CMN_status=$?
                            if [ $CMN_status -ne 0 ]; then
                               CMN_m1="'$CMN_cmd' \"$CMN_file\"'"
                               CMN_m2="ERROR=$CMN_stderr."
                               CMN_m3="$script_name terminated."
                               CMN_m4=`ls -al "$CMN_file"|sed "s/^/$sp/"`
                               echo "`date '+%Y-%m-%d %T'` FATAL ERROR: $CMN_ID" 1>&2
                               echo "${sp}$CMN_m1"                               1>&2
                               echo "${sp}$CMN_m2"                               1>&2
                               echo "${sp}$CMN_m3 File list (if any) follows:"   1>&2
                               echo "${sp}$dashes"                               1>&2
                               echo "$CMN_m4"                                    1>&2

                               if [ $CMN_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                                  WRITE_ERR_TO_SYSLOGS -p "$CMN_pri" -t "$CMN_tag" \
                                    "ABORT: $CMN_ID $CMN_m1 $CMN_m2 $CMN_m3"
                               fi
                               EXIT 1
                            fi # if [ $CMN_status -ne 0 ]; then
                         else
                            CMN_m1="Skipping '$CMN_cmd \"$CMN_file\"'"
                            CMN_m2="$CMN_file not writable!"
                            CMN_m3="$script_name continues."
                            echo "`date '+%Y-%m-%d %T'` WARNING: $CMN_ID" 1>&2
                            echo "${sp}$CMN_m1"                           1>&2
                            echo "${sp}$CMN_m2"                           1>&2
                            echo "${sp}$CMN_m3"                           1>&2
                         fi # if [ -w $CMN_file ]; then, else
                      done # while read CMN_file
                   return 0
                fi # if [ ."$CMN_args" != . ]; then

                #--------------------------------------------------------------------#
                # Reaching this point means we have an insufficient arguments error. #
                #--------------------------------------------------------------------#
                CMN_m1="Insufficient args."
                CMN_m2="$script_name terminated."
                echo "`date '+%Y-%m-%d %T'` FATAL ERROR (Function): $CMN_ID"  1>&2
                echo "${sp}$CMN_m1.\n"                                        1>&2
                echo "${sp}Usage: $CMN_name -l -p priority -t tag -v"         1>&2
                echo "${sp}                      permissions fileid [...]\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}$CMN_m2"                                           1>&2
                if [ $CMN_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                   WRITE_ERR_TO_SYSLOGS -p "$CMN_pri" -t "$CMN_tag" \
                     "ABORT: $CMN_ID $CMN_m1 $CMN_m2"
                fi

                EXIT 1
              } # "CMN_" 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: chmod_exit_err_nomail.sh                                #
              #                                                                      #
              #       Usage: CHMOD_EXIT_ERR_NOMAIL -l -p priority -t tag -v \        #
              #                                          permissions fileid ...      #
              #                                                                      #
              #                                    -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: Change permission of one or more files, reporting       #
              #              errors via stderr and logger, and exiting on error.     #
              #                                                                      #
              # Description: This function adds no new functionality to the UNIX     #
              #              chmod command beyond automatic notification/logging     #
              #              before exiting on error.  Unlike CHMOD_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).              #
              #                                                                      #
              #     Globals: No global variables assigned from this function.        #
              #              "CMN_" prefix identifies local function variables.      #
              #                                                                      #
              #       Calls: EXIT library function.  (DOES NOT use EMAIL_MSG         #
              #              library function for error reporting/notification.)     #
              #              Optionally, WRITE_ERR_TO_SYSLOGS is also called.        #
              #                                                                      #
              # Exit_status: Exits with failure (1) on error.  This is because       #
              #              any error while changing file permissions is            #
              #              potentially a show stopper.  Else, returns 0.           #
              #                                                                      #
              #       Notes: .....................................................   #
              #              .....................................................   #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.19 * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.18 * 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