# SccsId[] = "%W% (USL function) %G%"
EE_name="ECHO_EXEC"
if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
[ ."`set|egrep '^$EE_name\(\)\{$'`" != . ] && EE_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 '/^'$EE_name'[=\(]?/'`" != . ] && EE_loaded=1
else # Linux
[ ."`typeset -F|awk '/^'$EE_name'[=\(]?/'`" != . ] && EE_loaded=1
fi
fi
if [ 0${EE_loaded} -eq 0 ]; then
#----------------------------------------------------------------------#
ECHO_EXEC() # 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/cp_dev_null_exit_err.sh # Function dependencies #
. $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}'`}
: ${sp:=" "}
: ${tmp:=/var/tmp}
EE_ID="$script_name($EE_name)"
if [ ."$log" = . ]; then
log=$name_root.log
[ ."$teelog" = . ] && teelog="cat"
fi
if [ ."$1" = . ]; then
EMAIL_MSG "ERROR (Function): $EE_ID" \
"${sp}Insufficient args." \
"${sp}Usage: $EE_name command-to-run." \
"${sp} command-to-run = command string (quoted as needed)."
return 255
fi
#--------------------------------------------------------------#
# If any of these are missing, assign the following defaults: #
#--------------------------------------------------------------#
: ${id_num=`id|sed 's/^\(uid=\)\([0-9]*\)\(.*\)/\2/'`}
: ${id_hex=`echo "obase=16;$id_num"|bc`}
: ${yymmddhhmiss:=`date '+%y''%m%d%H''%M''%S'`}
: ${Xtimestamp:=`echo "obase=16;$yymmddhhmiss+$$"|bc`}
: ${stdout_root:=$tmp/$name_root"_s1_"$id_hex"."}
: ${stdout:=${stdout_root}$Xtimestamp}
: ${stderr_root:=$tmp/$name_root"_s2_"$id_hex"."}
: ${stderr:=${stderr_root}$Xtimestamp}
CP_DEV_NULL_EXIT_ERR $stdout $stderr # Zero/Clear these dogs.
#------------------------------------------------------------#
# When running anything OTHER THAN an 'su' command, use the #
# normal '$*' (without the quotes). However, for the 'su', #
# use the special '"$@"' variable (with double (") quotes), #
#------------------------------------------------------------#
if [ `echo $1|$AWK '{$1!="su"?x=1:x=0;print x;exit x}'` ]; then
echo "`date '+%Y-%m-%d %T'` "'$*='"$*" | $teelog
[ ."$EE_test_run" = . -o $EE_TEST_RUN -eq 0 ] \
&& $* >> $stdout 2> $stderr \
|| status=0 # If not test run, run command
else
echo "`date '+%Y-%m-%d %T'` "'$@='"$@" | $teelog
[ ."$EE_test_run" = . -o $EE_TEST_RUN -eq 0 ] \
&& "$@" >> $stdout 2> $stderr \
|| status=0 # If not test run, run command
fi
EE_status=$?
#----------------------------------------------#
# A feeble attempt to ensure these are closed. #
#----------------------------------------------#
touch $stdout $stderr > /dev/null 2>&1
return $EE_status
} # "EE_" prefix identifies this function's local variables.
fi
#======================================================================#
# D O C U M E N T A T I O N #
#======================================================================#
# #
# Author: Bob Orlando #
# #
# Date: December 14, 1996 #
# #
# Program ID: echo_exec.sh #
# #
# Usage: ECHO_EXEC command-string #
# #
# Purpose: Echo and execute a command (script, executable, #
# whatever), sending any output to $stdout and $stderr #
# files. #
# #
# Globals: Expects $stdout and $stderr to be assigned. #
# If unassigned, defaults are used. #
# "EE_" prefix identifies local function variables. #
# #
# Exit_status: Returns 255 status for function error (e.g. user #
# supplies insufficient number of arguments). Otherwise, #
# returns command run/exit status. #
# #
# Calls: EMAIL_MSG and CP_DEV_NULL_EXIT_ERR library functions. #
# #
# Notes: The variable, $* is used in every case except when #
# the command is an 'su'. Then, "$@" is used. #
# #
# Modified: 2007-03-27 Bob Orlando #
# v1.12 * Add test for $EE_test_run var assignment. #
# If set, we simply return zero. #
# #
# 2004-04-02 Bob Orlando #
# v1.11 * Expand $AWK testing and assignment. #
# #
# 2004-03-03 Bob Orlando #
# v1.10 * Change set|egrep|awk to just set|egrep. #
# #
#----------------------------------------------------------------------#
|