# SccsId[] = "%W% (USL function) %G%"
CON_name="CHOWN_EXIT_ERR_NOMAIL"
if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
[ ."`set|egrep '^$CON_name\(\)\{$'`" != . ] && CON_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 '/^'$CON_name'[=\(]?/'`" != . ] && CON_loaded=1
else # Linux
[ ."`typeset -F|awk '/^'$CON_name'[=\(]?/'`" != . ] && CON_loaded=1
fi
fi
if [ 0${CON_loaded} -eq 0 ]; then
#----------------------------------------------------------------------#
CHOWN_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
CON_ID="$script_name($CON_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). #
#--------------------------------------------------------------------#
CON_options="lptv"
CON_opt_l=0
CON_opt_p=0
CON_opt_t=0
CON_opt_v=0
CON_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
CON_tag="$logger_t" # within library defaults.sh.
CON_opts=""
CON_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. #
#-------------------------------------------------------------#
CON_n=`expr "$1". : '-.*'`
if [ $CON_n -gt 0 ]; then
if [ $CON_n -eq 2 ]; then
CON_optargs="$1"
else
CON_optargs=`echo "$1"|sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
fi
for CON_optarg in $CON_optargs
do
case $CON_optarg in
-l ) CON_opt_l=1
. $SHLIB/write_err_to_syslogs.sh
;;
-p ) if [ $CON_opt_l -eq 1 -a $CON_opt_p -eq 0 ]; then
CON_opt_p=1
if [ -`expr "$2" : '\(\-\)['$CON_options']'` != "--" ]; then
CON_pri="$2"
shift
fi
else
CON_opts="$CON_opts $CON_optarg"
fi
;;
-t ) if [ $CON_opt_l -eq 1 -a $CON_opt_t -eq 0 ]; then
CON_opt_t=1
CON_tag="$2"
if [ -`expr "$2" : '\(\-\)['$CON_options']'` != "--" ]; then
CON_tag="$2"
shift
fi
else
CON_opts="$CON_opts $CON_optarg"
fi
;;
-v ) CON_opt_v=1
;;
esac
done # for CON_optarg in $CON_optargs
else
[ ."$CON_args" != . ] \
&& CON_args="$CON_args|$1" \
|| CON_args="$1"
fi
shift
done # while [ ."$1" != . ]
CON_opts=`expr "$CON_opts" : ' *\(.*\)'` # Strip leading blanks
CON_args=`expr "$CON_args" : '|*\(.*\)'` # Strip leading stiles
#----------------------------------------------------------#
# If we still have args, then the first MUST be the owner. #
#----------------------------------------------------------#
CON_status=0
if [ ."$CON_args" != . ]; then
CON_owner=""
echo "$CON_args"|awk -F\| '{for (i=1;i<=NF;i++) print $i}' \
| while read CON_file
do
if [ ."$CON_owner" = . ]; then
CON_owner="$CON_file" # 1st arg MUST be the owner.
CON_cmd="chown $CON_opts $CON_owner"
continue # Got it, so iterate our for-loop.
fi
if [ ! \( -f "$CON_file" -o -d "$CON_file" \) ]; then
CON_m1="Skipping $CON_cmd '$CON_file'."
CON_m2="$CON_file not found!"
CON_m3="$script_name continues."
echo "`date '+%Y-%m-%d %T'` WARNING: $CON_ID" 1>&2
echo "${sp}$CON_m1" 1>&2
echo "${sp}$CON_m2" 1>&2
echo "${sp}$CON_m3" 1>&2
elif [ -w "$CON_file" ]; then # If file is writable, then do ...
[ $verbose -eq 1 -o $CON_opt_v -eq 1 ] \
&& echo "`date '+%Y-%m-%d %T'` $CON_cmd \"$CON_file\"" \
| $teelog
CON_stderr=`$CON_cmd "$CON_file" 2>&1`
CON_status=$?
if [ $CON_status -ne 0 ]; then
CON_m1="'$CON_cmd \"$CON_file\"'"
CON_m2="ERROR=$CON_stderr."
CON_m3="$script_name terminated."
CON_m4=`ls -al "$CON_file"|sed "s/^/$sp/"`
echo "`date '+%Y-%m-%d %T'` FATAL ERROR: $CON_ID" 1>&2
echo "${sp}$CON_m1" 1>&2
echo "${sp}$CON_m2" 1>&2
echo "${sp}$CON_m3 File list (in any) follows:" 1>&2
echo "${sp}$dashes" 1>&2
echo "$CON_m4" 1>&2
if [ $CON_opt_l -eq 1 ]; then # Logger option? (1 = yes)
WRITE_ERR_TO_SYSLOGS -p "$CON_pri" -t "$CON_tag" \
"ABORT: $CON_ID $CON_m1 $CON_m2 $CON_m3"
fi
EXIT 1
fi # if [ $CON_status -ne 0 ]; then
else
CON_m1="Skipping '$CON_cmd \"$CON_file\"'"
CON_m2="$CON_file not writable!"
CON_m3="$script_name continues."
echo "`date '+%Y-%m-%d %T'` WARNING: $CON_ID" 1>&2
echo "${sp}$CON_m1" 1>&2
echo "${sp}$CON_m2" 1>&2
echo "${sp}$CON_m3" 1>&2
fi # if [ -w $CON_file ]; then, else
done # while read CON_file
return 0
fi # if [ ."$CON_args" != . ]; then
#--------------------------------------------------------------------#
# Reaching this point means we have an insufficient arguments error. #
#--------------------------------------------------------------------#
CON_m1="Insufficient args."
CON_m2="$script_name terminated."
echo "`date '+%Y-%m-%d %T'` FATAL ERROR (Function): $CON_ID" 1>&2
echo "${sp}$CON_m1.\n" 1>&2
echo "${sp}Usage: $CON_name -l -p priority -t tag -v" 1>&2
echo "${sp} 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}$CON_m2" 1>&2
if [ $CON_opt_l -eq 1 ]; then # Logger option? (1 = yes)
WRITE_ERR_TO_SYSLOGS -p "$CON_pri" -t "$CON_tag" \
"ABORT: $CON_ID $CON_m1 $CON_m2"
fi
EXIT 1
} # "CON_" 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: chown_exit_err.sh #
# #
# Usage: CHOWN_EXIT_ERR_NOMAIL -l -p priority -t tag -v \ #
# 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 ownership on one or more files, reporting #
# errors via stderr and logger, and exiting on error. #
# #
# Description: This function adds no new functionality to the UNIX #
# chown command beyond automatic notification/logging #
# before exiting on error. Unlike CHOWN_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. #
# "CON_" 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 changing file permissions (ownership) #
# is potentially a show stopper. Else, returns 0. #
# #
# Notes: ..................................................... #
# ..................................................... #
# #
# Modified: 2004-04-02 Bob Orlando #
# v1.17 * Expand $AWK testing and assignment. #
# #
# 2004-03-03 Bob Orlando #
# v1.16 * Change set|egrep|awk to just set|egrep. #
# #
#----------------------------------------------------------------------#
|