# SccsId[] = "%W% (USL function) %G%"
CD_name="CHDIR_EXIT_ERR"
if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
[ ."`set|egrep '^$CD_name\(\)\{$'`" != . ] && CD_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 '/^'$CD_name'[=\(]?/'`" != . ] && CD_loaded=1
else # Linux
[ ."`typeset -F|awk '/^'$CD_name'[=\(]?/'`" != . ] && CD_loaded=1
fi
fi
if [ 0${CD_loaded} -eq 0 ]; then
#----------------------------------------------------------------------#
CHDIR_EXIT_ERR() # 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/email_msg.sh # Calls $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:=" "}
: ${tmp:=/var/tmp}
if [ ."$log" = . ]; then
log=$name_root.log
[ ."$teelog" = . ] && teelog="cat"
fi
CD_ID="$script_name($CD_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). #
#--------------------------------------------------------------------#
CD_options="lptv"
CD_opt_l=0
CD_opt_p=0
CD_opt_t=0
CD_opt_v=0
CD_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
CD_tag="$logger_t" # within library defaults.sh.
CD_opts=""
CD_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. #
#-------------------------------------------------------------#
CD_n=`expr "$1". : '-.*'`
if [ $CD_n -gt 0 ]; then
if [ $CD_n -eq 2 ]; then
CD_optargs="$1"
else
CD_optargs=`echo "$1"|sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
fi
for CD_optarg in $CD_optargs
do
case $CD_optarg in
-l ) CD_opt_l=1
. $SHLIB/write_err_to_syslogs.sh
;;
-p ) if [ $CD_opt_l -eq 1 -a $CD_opt_p -eq 0 ]; then
CD_opt_p=1
if [ -`expr "$2" : '\(\-\)['$CD_options']'` != "--" ]; then
CD_pri="$2"
shift
fi
else
CD_opts="$CD_opts $CD_optarg"
fi
;;
-t ) if [ $CD_opt_l -eq 1 -a $CD_opt_t -eq 0 ]; then
CD_opt_t=1
CD_tag="$2"
if [ -`expr "$2" : '\(\-\)['$CD_options']'` != "--" ]; then
CD_tag="$2"
shift
fi
else
CD_opts="$CD_opts $CD_optarg"
fi
;;
-v ) CD_opt_v=1
;;
esac
done # for CD_optarg in $CD_optargs
else
[ ."$CD_args" != . ] \
&& CD_args="$CD_args $1" \
|| CD_args="$1"
fi
shift
done # while [ ."$1" != . ]
CD_opts=`expr "$CD_opts" : ' *\(.*\)'` # Strip leading blanks
#----------------------------------------------------------------#
# If we have sufficient arguments, then attempt our cd command. #
#----------------------------------------------------------------#
CD_status=0
CD_cmd="cd $CD_opts"
if [ ."$CD_args" != . ]; then
#-----------------------------------------------------------#
# If the directory exists, make it our currenr working dir. #
#-----------------------------------------------------------#
if [ -d "$CD_args" ]; then # If the directory exists, ...
[ $verbose -eq 1 -o $CD_opt_v -eq 1 ] \
&& echo "`date '+%Y-%m-%d %T'` cd \"$CD_args\"" | $teelog
CD_stderr=`$CD_cmd "$CD_args" 2>&1`
CD_status=$?
if [ $CD_status -eq 0 ]; then
$CD_cmd "$CD_args" 2>&1
return 0
fi
else # Oops. Got a problem here.
CD_stderr="'$CD_args' directory does not exist!"
CD_status=1
fi
fi # if [ ."$CD_args" != . ]; then
#----------------------------------------------------#
# Reaching this point means that something is amiss. #
#----------------------------------------------------#
if [ $CD_status -ne 0 ]; then
CD_m1="Problems cd'ng to \"$CD_args\""
CD_m2="ERROR=$CD_stderr."
CD_m3="$script_name terminated."
EMAIL_MSG "FATAL ERROR: $CD_ID" \
"${sp}$CD_m1" \
"${sp}$CD_m2" \
"${sp}$CD_m3"
if [ $CD_opt_l -eq 1 ]; then # Logger option? (1 = yes)
WRITE_ERR_TO_SYSLOGS -p "$CD_pri" -t "$CD_tag" \
"ABORT: $script_name:$CD_name(cd $CD_dir)" \
"$CD_m1 $CD_m2 $CD_m3"
fi
EXIT 1
fi # if [ $CD_status -ne 0 ]; then
#--------------------------------------------------------------------#
# Reaching this point with zero status means insufficient args error.#
#--------------------------------------------------------------------#
CD_m1="Insufficient args."
CD_m2="$script_name terminated."
EMAIL_MSG "FATAL ERROR (Function): $CD_ID" \
"${sp}$CD_m1.\n" \
"${sp}Usage: $CD_name -l -p priority -t tag -v" \
"${sp} directory\n" \
"${sp} -l = Notify via logger command" \
"${sp} (default is false)" \
"${sp} -p = logger priority string" \
"${sp} (default is 'user.err')" \
"${sp} -t = logger tag string" \
"${sp} (default is '$LOGNAME[$$]')" \
"${sp} -v = Verbose\n" \
"${sp}Option notes: Option (-l) MUST precede -p and -t." \
"${sp}$CD_m2"
if [ $CD_opt_l -eq 1 ]; then # Logger option? (1 = yes)
WRITE_ERR_TO_SYSLOGS -p "$CD_pri" -t "$CD_tag" \
"ABORT: $CD_ID $CD_m1 $CD_m2"
fi
EXIT 1
} # "CD_" 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: January 31, 1997 #
# #
# Program ID: chdir_exit_err.sh #
# #
# Usage: CHDIR_EXIT_ERR -l -p priority -t tag -v directory #
# #
# -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 working directory to specified directory, #
# reporting errors via email and logger, and exiting #
# on error. #
# #
# Description: This function adds no new functionality to the UNIX #
# cd command beyond automatic notification/logging #
# before exiting on error. Notification is provided #
# via the EMAIL_NOTIFY 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 #
# passed (both have defaults if omitted). #
# #
# Globals: No global variables assigned from this function. #
# "CD_" prefix identifies local function variables. #
# #
# Calls: EMAIL_MSG and EXIT library functions. #
# Optionally, WRITE_ERR_TO_SYSLOGS is also called. #
# #
# Exit_status: Exits with failure (1) on error. This is because #
# any error while changing directory is potentially #
# a show stopper. #
# #
# Notes: ..................................................... #
# ..................................................... #
# #
# Modified: 2004-04-02 Bob Orlando #
# v1.16 * Expand $AWK testing and assignment. #
# #
# 2004-03-03 Bob Orlando #
# v1.15 * Change set|egrep|awk to just set|egrep. #
# #
#----------------------------------------------------------------------#
|