标准shell script示例

#!/bin/ksh
############################################################
##NAME            -

##DESCRIPTION     - ##
##FUNCTION CALLS DEFINED
## - cleanup      -

## - isql_exec    -

## - isql_set     -

## - message      -

##                 

## - check_env    - ##                 

## - check_sql    - ##
##REVISION HISTORY
## ##
############################################################

PRG="`whence \"$0\"`" >/dev/null 2>&1
M_HOME="`dirname \"$PRG\"`/.."
 
while [ -h "$PRG" ]; do
        # Get the target of the symlink.  N.B.:  We assume that neither the
        # link's value nor the pathname leading to it contains "-> ".
        ls=`/usr/bin/ls -ld "$PRG"`
        link=`/usr/bin/expr "$ls" : '^.*-> \(.*\)$'`
        # If the link is absolute, use it as is; otherwise, substitute it
        # into the leafname part of $PRG.
        if /usr/bin/expr "$link" : '^/' > /dev/null; then
                prg="$link"
        else
                prg="`/usr/bin/dirname \"$PRG\"`/$link"
        fi
        PRG=`whence "$prg"` >/dev/null 2>&1
        M_HOME=`/usr/bin/dirname "$PRG"`/..
done
 
cd ${M_HOME}
export MBASE="`pwd`"
echo "MBASE=${MBASE}"

cd $MBASE/..
export PARDIR="`pwd`"

. $PARDIR/HNWEnv/HNWGlobal.env

cd $MBASE/tools

 
export BASENAME="`basename $0`"
export PID="$$"
export UNAME="`/usr/bin/uname -n`"
export ERR_LEVEL=${ERR_LEVEL:-1}


############################################################
## Cleanup and exit
############################################################
cleanup() {
##
## Check for trap execution and set appropriate $MESSAGE
##
EXIT="${1:-9999}"
if [ ${EXIT} -eq 0 ];then
   echo "FINISHED:Exiting with code:${EXIT} at `date`" |${TEE}
   MESSAGE=${MESSAGE:-"Normal exit of ${BASENAME}"}
   logger -p user.err "SUCCESS:${BASENAME} successfully completed"
 
# for name in `cat /home/osadm/etc/beeplist` ; do
# /home/osadm/bin/beeper $name 00011111
# done

fi

if [ ${EXIT} -eq 9999 ];then
   echo "EXITING:CAUGHT TRAP WITH CODE:${EXIT} AT `date`" |${TEE}
   logger -p user.err "WARNING:${BASENAME} CAUGHT TRAP ${EXIT} AT `date`" \
   |${TEE}
   MESSAGE=${MESSAGE:-"Caught trap with code:${EXIT} at `date`"}
 
 #for name in `cat /home/osadm/etc/beeplist` ; do
 #/home/osadm/bin/beeper $name 00019999
 #done

fi

if [ ${EXIT} -ne 0 -a ${EXIT} -ne 9999 ];then
   logger -p user.err "FATAL:${MESSAGE}"
   echo "Abnormal exit of ${BASENAME} at `date`" |${TEE}
   MESSAGE=${MESSAGE:-"Abnormal exit of ${BASENAME} at `date`"}
 
#for name in `cat /home/osadm/etc/beeplist` ; do
# /home/osadm/bin/beeper $name 00019999
#done

fi

export MESSAGE

message
##
## Remove all log and tmp files
## and unmount /mnt
compress ${SYB_LOG}/*.dat.* >/dev/null 2>&1
rm -f ${LOGFILE} ${LOGFILE}- >/dev/null 2>&1
rm -f ${TMPFILE} ${TMPFILE}- >/dev/null 2>&1
rm -f ${ERRFILE} ${ERRFILE}- >/dev/null 2>&1
rm -f ${SQLFILE} ${SQLFILE}- >/dev/null 2>&1
exit ${EXIT}
}

############################################################
## execute isql commands
## If $SQL_NO_PARSE is set, $SQLFILE will not be parsed
## if $SQL_NO_ERR is set, No error will be returned
############################################################
isql_exec() {
##
## Create isql commands
## and redirect sql command file
## into $ISQL
##
##
echo "use ${SQL_DB}" > ${SQLFILE}
echo "go"            >> ${SQLFILE}
while [ $# -ne 0 ];do
   echo "${1}"       >> ${SQLFILE}
   echo "go"         >> ${SQLFILE}
   shift
done
echo "quit"          >> ${SQLFILE}
${ISQL} >${SQLFILE}-<${SQLFILE}
##
## Check for errors and isql return
## Parse out 1st and last two lines
##
SQL_ERR1=$?
cat ${SQLFILE}-
SQL_ERR2="`cat ${SQLFILE}- |egrep \
'(ERROR|LIBRARY|failed|cannot)' |egrep -v 'NOTICE' |wc -l`"
if [ ${SQL_ERR1} -ne 0 -o ${SQL_ERR2} -ge 1 ];then
   if [ ! "${SQL_NO_ERR}" ];then
      echo "FATAL:isql returned an error" |${TEE}
      MESSAGE="FATAL ${BASENAME}:isql returned an error";export MESSAGE
      logger -p user.err "${MESSAGE}"
      return 1
   fi
else
   cat ${SQLFILE}- |sed '1d' |sed '1d' >${SQLFILE}
   cat ${SQLFILE}  |sed '$d' |sed '$d' >${SQLFILE}-
fi
echo ${SQLFILE}
##
## Check if output contains reserved
## words for system SQL procedures
##
NUMBER="`cat ${SQLFILE}- |egrep 'ROWCOUNT ' |wc -l`"
unset SQL_ROW
if [ ${NUMBER} -eq 1 ];then
   SQL_ROW="`cat ${SQLFILE}- |egrep 'ROWCOUNT ' |awk '{ print $2 }'`"
   export SQL_ROW
   SQL_ERR1="`cat ${SQLFILE}- |egrep 'ROWCOUNT ' |awk '{ print $4 }'`"
   SQL_ERR1=${SQL_ERR1:-0}
   export SQL_ERR1
   cat ${SQLFILE}- |sed '$d' > ${SQLFILE}
   mv -f ${SQLFILE} ${SQLFILE}-
   SQL_NUM="`cat ${SQLFILE}- |wc -l`";export SQL_NUM
   if [ ${SQL_NUM} -ne ${SQL_ROW} -o ${SQL_ERR1} -ne 0 ];then
      if [ ! "${SQL_NO_ERR}" ];then
         echo "FATAL:isql returned an error" |${TEE}
         MESSAGE="FATAL ${BASENAME}:isql returned an error";export MESSAGE
         logger -p user.err "${MESSAGE}"
         return 1
      fi
   fi
fi
##
## (No)Parse into ${SQLFILE} for output
## Check if ${SQL_NO_PARSE} is set
##
SQL_NUM="`cat ${SQLFILE}- |wc -l`";export SQL_NUM
if [ "${SQL_NO_PARSE}" ];then
   cat ${SQLFILE}- |egrep -v '\-----' > ${SQLFILE}
else
   cat ${SQLFILE}- |sed 's/ //g' |sed 's/NULL//g' \
   |egrep -v '\-----' > ${SQLFILE}
fi
return 0

}

############################################################
## Set $ISQL
## $1 - dbname
## $2 - user
## $3 - passwd
## $4 - server
############################################################
isql_set() {
##
## SQL specific info
##
if [ $# -ne 4 ];then
   echo "FATAL:isql_set returned an error" |${TEE}
   MESSAGE="FATAL ${BASENAME} :isql_set returned an error";export MESSAGE
   logger -p user.err "${MESSAGE}"
  return 1
fi
SQL_DB=${1};    export SQL_DB
SQL_USER=${2};  export SQL_USER
SQL_PASSWD=${3};export SQL_PASSWD
SQL_GOLDEN=${4};export SQL_GOLDEN
ISQL="${SYB_BIN}/isql -U${SQL_USER} -P${SQL_PASSWD} -S${SQL_GOLDEN} -X -s: -w512"
export ISQL
}

############################################################
## Send UNIX mail
############################################################
message() {
##
## If $LOGFILE exists, return FATAL, WARNING or ALL errors
## else indicate abnormal exit
##
if [ -f ${LOGFILE} ];then
   if [ ${ERR_LEVEL} -eq 2 ];then
      cat ${LOGFILE} |head -4 > ${LOGFILE}-
      cat ${LOGFILE} |egrep WARNING >> ${LOGFILE}-
      cat ${LOGFILE} |tail -1 >> ${LOGFILE}-
      mv -f ${LOGFILE}- ${LOGFILE}
   fi
   if [ ${ERR_LEVEL} -eq 3 ];then
       cat ${LOGFILE} |head -4 > ${LOGFILE}-
       cat ${LOGFILE} |egrep FATAL >> ${LOGFILE}-
       cat ${LOGFILE} |tail -1 >> ${LOGFILE}-
       mv -f ${LOGFILE}- ${LOGFILE}
   fi
   /usr/ucb/Mail -s "${MESSAGE}" ${MAIL_LIST} < ${LOGFILE}
else
   echo "Abnormal exit of ${BASENAME}" >> /tmp/help.${PID}
   /usr/ucb/Mail -s "${MESSAGE}" jl1568 < /tmp/help.${PID}
   rm -rf /tmp/help.${PID} >>/dev/null 2>&1
fi
}

############################################################
## Check for environment
############################################################
check_env() {
##
## Get my arch and set arch
## specific variables
##
/usr/ucb/renice -20 -p ${PID} > /dev/null 2>&1
LOGFILE="${SYB_LOG}/${BASENAME}-${PID}-log"; export LOGFILE
TMPFILE="${SYB_LOG}/${BASENAME}-${PID}-tmp"; export TMPFILE
ERRFILE="${SYB_LOG}/${BASENAME}-${PID}-err"; export ERRFILE
SQLFILE="${SYB_LOG}/${BASENAME}-${PID}-sql"; export SQLFILE
TEE="/usr/bin/tee -a ${LOGFILE}";export TEE
if [ -r ${MAIL_DIR}/maillist.txt ];then
   MAIL_LIST=${MAIL_LIST:-"`cat ${MAIL_DIR}/maillist.txt`"};export MAIL_LIST
fi
##
## Display starting messages
##
echo "STARTED:${BASENAME} at `date` on ${UNAME}" |${TEE}
echo "USER:`/bin/logname`" |${TEE}
echo "COMMAND:${COMMAND}" |${TEE}
echo "LOGFILE:${LOGFILE}" |${TEE}
echo " " > ${TblStatusLog}
}

############################################################
## Test sql connection
############################################################
check_sql() {
if [ ! -x ${SYB_BIN}/isql -a ! -r ${SYBASE}/interfaces ];then
   echo "FATAL:SYBASE IS NOT INSTALLED" |${TEE}
##   logger -p user.err "${MESSAGE}"
   MESSAGE="FATAL:SYBASE IS NOT INSTALLED";export MESSAGE
   cleanup 1
fi
if [ ! -x ${SYB_BIN}/bcp ];then
   echo "FATAL:SYBASE BCP IS NOT INSTALLED" |${TEE}
##   logger -p user.err "${MESSAGE}"
   MESSAGE="FATAL:SYBASE BCP IS NOT INSTALLED";export MESSAGE
   cleanup 1
fi
if [ ! -w ${SYB_LOG} ];then
   echo "FATAL:${SYB_LOG} is not writable" |${TEE}
##   logger -p user.err "${MESSAGE}"
   MESSAGE="FATAL:${SYB_LOG} is not writable";export MESSAGE
   cleanup 1
fi
isql_exec "sp_who"
if [ $? -ne 0 ];then
   echo "FATAL:${SQL_GOLDEN} IS DOWN" |${TEE}
##   logger -p user.err "${MESSAGE}"
   MESSAGE="FATAL:${SQL_GOLDEN} IS DOWN"
   cleanup 1
fi
}

############################################################
## Main
############################################################
##
## Check for usage Parse command line arguments
##
trap cleanup 1 2 9 15
check_env

isql_set $SYBDB $SYBUSER $SYBPASS $SYBSVR

isql_exec "insert TblHNWBatchStatus values ( convert (int, convert(char(20),getdate(),112)),'updateHNWTableStatistics01','JOB BEGIN',getdate()) "

isql_exec "exec UpdHNWIndexStatistics "

isql_exec "insert TblHNWBatchStatus values ( convert (int, convert(char(20),getdate(),112)),'exec store procedure UpdHNWIndexStatistics','JOB BEGIN',getdate()) "

isql_exec "insert TblHNWBatchStatus values ( convert (int, convert(char(20),getdate(),112)),'update statistics TblHNWSecr','JOB BEGIN',getdate()) "

if [ $? -ne 0 ];then
   echo "FATAL:${SQL_GOLDEN} IS DOWN" |${TEE}
   logger -p user.err "${MESSAGE}"
   MESSAGE="FATAL:${SQL_GOLDEN} IS DOWN"
   cleanup 1
fi

isql_exec "insert TblHNWBatchStatus values ( convert (int, convert(char(20),getdate(),112)),'updateHNWTableStatistics01','JOB END',getdate()) "

cleanup 0
exit 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值