# SccsId[] = "%W% (USL function) %G%"
CPN_name="CP_EXIT_ERR_NOMAIL"
if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
[ ."`set|egrep '^$CPN_name\(\)\{$'`" != . ] && CPN_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 '/^'$CPN_name'[=\(]?/'`" != . ] && CPN_loaded=1
else # Linux
[ ."`typeset -F|awk '/^'$CPN_name'[=\(]?/'`" != . ] && CPN_loaded=1
fi
fi
if [ 0${CPN_loaded} -eq 0 ]; then
#----------------------------------------------------------------------#
CP_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[$$]"}
: ${sp:=" "}
: ${tmp:=/var/tmp}
if [ ."$log" = . ]; then
log=$name_root.log
[ ."$teelog" = . ] && teelog="cat"
fi
CPN_ID="$script_name($CPN_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). #
#--------------------------------------------------------------------#
CPN_options="lpt"
CPN_opt_l=0
CPN_opt_p=0
CPN_opt_t=0
CPN_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
CPN_tag="$logger_t" # within library defaults.sh.
CPN_opts=""
CPN_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. #
#-------------------------------------------------------------#
CPN_n=`expr "$1". : '-.*'`
if [ $CPN_n -gt 0 ]; then
if [ $CPN_n -eq 2 ]; then
CPN_optargs="$1"
else
CPN_optargs=`echo "$1"|sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
fi
for CPN_optarg in $CPN_optargs
do
case $CPN_optarg in
-l ) CPN_opt_l=1
. $SHLIB/write_err_to_syslogs.sh
;;
-p ) if [ $CPN_opt_l -eq 1 -a $CPN_opt_p -eq 0 ]; then
CPN_opt_p=1
if [ -`expr "$2" : '\(\-\)['$CPN_options']'` != "--" ]; then
CPN_pri="$2"
shift
fi
else
CPN_opts="$CPN_opts $CPN_optarg"
fi
;;
-t ) if [ $CPN_opt_l -eq 1 -a $CPN_opt_t -eq 0 ]; then
CPN_opt_t=1
CPN_tag="$2"
if [ -`expr "$2" : '\(\-\)['$CPN_options']'` != "--" ]; then
CPN_tag="$2"
shift
fi
else
CPN_opts="$CPN_opts $CPN_optarg"
fi
;;
* ) # Must be cp option; just pass it along.
CPN_opts="$CPN_opts $CPN_optarg"
shift
;;
esac
done # for CPN_optarg in $CPN_optargs
else
[ ."$CPN_args" != . ] \
&& CPN_args="$CPN_args|$1" \
|| CPN_args="$1"
fi
shift
done # while [ ."$1" != . ]
CPN_opts=`expr "$CPN_opts" : ' *\(.*\)'` # Strip leading blanks
CPN_args=`expr "$CPN_args" : '|*\(.*\)'` # Strip leading stiles
#------------------------------------------------------------#
# If we have sufficient args, then attempt our copy command. #
#------------------------------------------------------------#
CPN_status=0
CPN_cmd="cp $CPN_opts"
CPN_from=`expr "$CPN_args" : '\(.*\)\|.*`
CPN_to=` expr "$CPN_args" : '.*\|\(.*\)`
CPN_args="$CPN_from"
if [ ."$CPN_args" != . ]; then
echo "$CPN_args"|awk -F\| '{for (i=1;i<=NF;i++) print $i}' \
| while read CPN_from
do
echo "`date '+%Y-%m-%d %T'`" \
"$CPN_cmd \"$CPN_from\" \"$CPN_to\"" | $teelog
CPN_stderr=`$CPN_cmd "$CPN_from" "$CPN_to" 2>&1`
CPN_status=$?
if [ $CPN_status -ne 0 ]; then
CPN_m1="'$CPN_cmd \"$CPN_from\" \"$CPN_to\"'"
CPN_m2="ERROR=$CPN_stderr"
CPN_m3="$script_name terminated."
echo "`date '+%Y-%m-%d %T'` FATAL ERROR: $CPN_ID" 1>&2
echo "${sp}$CPN_m1" 1>&2
echo "${sp}$CPN_m2" 1>&2
echo "${sp}$CPN_m3" 1>&2
if [ $CPN_opt_l -eq 1 ]; then # Logger option? (1 = yes)
WRITE_ERR_TO_SYSLOGS -p "$CPN_pri" -t "$CPN_tag" \
"ABORT: $CPN_ID $CPN_m1 $CPN_m2 $CPN_m3"
fi
EXIT 1
fi # if [ $CPN_status -ne 0 ]; then
done # while read CPN_from
return 0
fi
#----------------------------------------------------------------#
# Reaching this point means we have an insufficient args error. #
#----------------------------------------------------------------#
CPN_m1="Insufficient args."
CPN_m2="$script_name terminated."
echo "`date '+%Y-%m-%d %T'` FATAL ERROR (Function): $CPN_ID" 1>&2
echo "${sp}$CPN_m1\n" 1>&2
echo "${sp}Usage: $CPN_name -l -p priority -t tag" 1>&2
echo "${sp} [cp-options] target destination\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[$$]')\n" 1>&2
echo "${sp}Option notes: Option (-l) MUST precede -p and -t." 1>&2
echo "${sp} 'cp-options' MUST follow function" 1>&2
echo "${sp} options." 1>&2
echo "${sp}$CPN_m2"
if [ $CPN_opt_l -eq 1 ]; then # Logger option? (1 = yes)
WRITE_ERR_TO_SYSLOGS -p "$CPN_pri" -t "$CPN_tag" \
"ABORT: $CPN_ID $CPN_m1 $CPN_m2"
fi
EXIT 1
} # "CPN_" 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: October 19, 1999 #
# #
# Program ID: cp_exit_err_nomail.sh #
# #
# Usage: CP_EXIT_ERR_NOMAIL -l -p priority -t tag \ #
# [cp-options] source destination #
# #
# -l = Notify via logger command #
# (default is false) #
# -p = Logger priority string #
# (default is "user.err") #
# -t = Logger tag string #
# (default is "$LOGNAME[$$]") #
# #
# Option notes: Option (-l) MUST precede -p and -t. #
# '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: Copy files/directories with error checking and #
# reporting and errors via stderr and logger, and #
# exiting on error. #
# #
# Description: This function adds no new functionality to the UNIX #
# cp command beyond automatic notification/logging #
# before exiting on error. Notification is provided #
# via echoes thru stderr. 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 passed (both have defaults #
# if omitted). #
# #
# Globals: No global variables assigned from this function. #
# "CPN_" prefix identifies local function variables. #
# #
# Calls: EXIT library function and, optionally, #
# WRITE_ERR_TO_SYSLOGS as directed. #
# #
# Exit_status: Exits with failure (1) on error. This is because #
# any error while copying a file is potentially a #
# show stopper. Else returns 0. #
# #
# Notes: ..................................................... #
# ..................................................... #
# #
# Modified: 2004-04-02 Bob Orlando #
# v1.14 * Expand $AWK testing and assignment. #
# * Bug fix: Add code to pass along other (cp) #
# options and while-do loop to process each #
# directory (should there be multiple source #
# directories) separately. #
# #
#----------------------------------------------------------------------#
|