【Shell脚本】根据起止日期获取Alert日志内容

【Shell脚本】根据起止日期获取Alert日志内容

根据输入的起止日期字符串,检索Oracle告警日志,打印中间的日志行内容。

#!/bin/bash
# $1 START_TIME_STR, e.g. "Oct 17 07:" 
# $2 END_TIME_STR, e.g. "Oct 17 08:"
source /home/oracle/.bash_profile
FILEPATH="$ORACLE_BASE/diag/rdbms/`hostname`/${ORACLE_SID}/trace/alert_${ORACLE_SID}.log"

retrieveLog()
{
  #echo $1,$2,$3
  lineStart=`grep -n "$1" "$3" | awk -F":" '{print $1}' | head -n1`
  lineEnd=`grep -n "$2" "$3" | awk -F":" '{print $1}' | tail -n1`

  echo "lineStart is ${lineStart}, lineEnd is ${lineEnd}."

  if [ -z "${lineStart}" ] || [ -z "${lineEnd}" ]
  then 
    echo "lineStart and lineEnd cannot be empty!"
  else
    echo "Retrieving the required log ..." 
    echo "########################################################"
    sed -n "${lineStart},${lineEnd}p" $3
    echo "########################################################"
    echo "Finished retrieving the required log ..."
  fi 
}

if [ -f "$FILEPATH" ]
then
  retrieveLog "$1" "$2" "$FILEPATH"
else
  echo "$FILEPATH does not EXIST! Please input the absolute log path."
  read -p "请输入日志文件路径: " LOGPATH
  retrieveLog "$1" "$2" "$LOGPATH"
fi

你可能感兴趣的:(Use4脚本,oracle,sql,shell)