#!/bin/ksh93 # IBM_PROLOG_BEGIN_TAG # This is an automatically generated prolog. # # 61haes_r714 src/43haes/lib/ksh93/common_functions.ksh93.sh 1.3 # # Licensed Materials - Property of IBM # # Restricted Materials of IBM # # COPYRIGHT International Business Machines Corp. 2005,2012 # All Rights Reserved # # US Government Users Restricted Rights - Use, duplication or # disclosure restricted by GSA ADP Schedule Contract with IBM Corp. # # IBM_PROLOG_END_TAG # @(#)27 1.3 src/43haes/lib/ksh93/common_functions.ksh93.sh, hacmp, 61haes_r714 7/7/12 14:59:27 # Load the common clmgr/clvt functions library . /usr/es/lib/ksh93/func_include [[ -z $SCRIPT_NAME ]] && SCRIPT_NAME=${0##*/} # Name: abort # # Description: Reports the error message indicated as the first # argument to this function, then exits the shell # provides a single point of exit # # Arguments: errno, # # Returns: n/a exits with errno as exit code # # Global Refs: None # function abort { typeset errcode=$1 errmsg $* exit $errcode } # Name: critical # # Description: If command fails, prints the command in the abort output. # Don't use this if a special message exists for this case # # Arguments: command line to execute # # Returns: 0 or exits program on error # # Global Refs: None # function critical { "$@" && return 0 abort 1 "$@" } # Name: errmsg # # Description: calls dspmsg with our catalog and default msgs # is tolerant of empty arg lists for ease of coding # # Arguments: [msg_number] # [args] # # Returns: Return code of dspmsg # # Global Refs: MSG # # Usage: errmsg [msg_number [arg ...] # function errmsg { typeset msgid=$1 shift if [[ -z $KLIB_MSGSET || -z $KLIB_MSGCATALOG ]]; then echo "Fatal Error: KLIB_MSGSET & KLIB_MSGCATALOG need to be set!" return 1 fi if (( $# )); then typeset -A arg index=0 for a in $*; do if [[ ${a:0:1} == "!" ]]; then name=${a:1:${#a}} typeset -n klist=$name arg[$index]=$klist else arg[$index]="$a" fi (( index=$index+1 )) done 2>/dev/null dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG $msgid "${MSG[$msgid]}" "${arg[0]}" "${arg[1]}" "${arg[2]}" "${arg[3]}" "${arg[4]}" "${arg[5]}" "${arg[6]}" "${arg[7]}" "${arg[8]}" "${arg[9]}" "${arg[10]}" "${arg[11]}" "${arg[12]}" "${arg[13]}" "${arg[14]}" "${arg[15]}" else dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG $msgid "${MSG[$msgid]}" fi return 0 } # # Name: logmsg # # Description: This function will log a message to a log file pointed # to by global KLIB_LOGFILE and to the console, # if KLIB_OUTPUT_CONSOLE=true # # Arguments: [msg_number] # [args] # # Global Refs: MSG # KLIB_MSGSET = message set # (1) # KLIB_MSGCATALOG = message catalog name db2sa.cat # KLIB_OUTPUT_CONSOLE = true / false # KLIB_DEFAULT_LOGFILE = fullpath to log file name # KLIB_HACMPLOG_ENTRY = HACMPlog entry to obtain path for # log file # # Usage: logmsg 100 "abc" 123 # function logmsg { typeset ARGS=$* if [[ -z $KLIB_DEFAULT_LOGFILE && -z $KLIB_HACMPLOG_ENTRY ]]; then echo "Fatal Error: KLIB_LOGFILE and KLIB_HACMPLOG_ENTRY not set!" return 1 fi LOGFILE=$KLIB_DEFAULT_LOGFILE if [[ -n $KLIB_HACMPLOG_ENTRY && -z KLIB_HACMPLOG_VALUE ]]; then odmget -q name=$KLIB_HACMPLOG_ENTRY | while IFS='=' read name value; do name=$(eval echo $name) if [[ "$name" == "value" ]]; then value=$(eval echo $value) KLIB_HACMPLOG_VALUE=$value fi done 2>/dev/null fi if [[ -n $KLIB_HACMPLOG_VALUE ]]; then LOGFILE=$KLIB_HACMPLOG_VALUE fi # Return the status code from calling errmsg (last statement is the ret code) touch $LOGFILE >/dev/null 2>&1 if [[ ! -f $LOGFILE ]]; then echo "Fatal Error: Unable to create log file $LOGFILE" return 1 fi if [[ "$KLIB_OUTPUT_CONSOLE" == "true" ]]; then errmsg $ARGS | tee -a $LOGFILE else errmsg $ARGS >> $LOGFILE fi } # # Name: dbgmsg # # Description synonym for logmsg, provides debug logging # by default this function ignores console logging # no console logging is enabled # # Arguments: [msg_number] # [args] # # Global Refs: MSG # KLIB_MSGSET = message set # (1) # KLIB_MSGCATALOG = message catalog name db2sa.cat # KLIB_OUTPUT_CONSOLE = true / false # KLIB_DEFAULT_LOGFILE = fullpath to log file name # KLIB_HACMPLOG_ENTRY = HACMPlog entry to obtain path for # log file # # Usage: dbgmsg 12 "foo" "bar" # function dbgmsg { logmsg $* } # Name: require # # Description: If command fails, calls abort with the rest of its args # # Arguments: command line to execute # optional about arguments # # Returns: 0 or exits program on error # # Global Refs: None # function require { eval $1 && return 0 shift abort 1 "$@" } # Name: require_dir # # Description: Special case of require because it is so common. # This also has is a special error message. This is to # simplify calling and reduce errors # # Arguments: Directory to create or ensure exists # # Returns: 0 or exits program on error # # Global Refs: None # function require_dir { mkdir -p $1 || abort 1 21 $1 }