将脚本记录到电子邮件

将脚本记录到电子邮件

我有一个脚本,可以扫描日志中的单词列表,然后发送包含结果的电子邮件。我想扩展此功能,对同一目录中的其他日志执行相同的操作,但将其与每个日志一起发送为新电子邮件。我该怎么做呢?

#!/bin/bash
LOG="pdb.lg psmseis.lg psyseis.lg"   # three logs to look through

REPORT_NAME='/tmp/pdb.lg_report'

LOGDIR="/pdb1"  # directory containing the log

# errors being searched for.

    LIST="
    abnormal
    allow*
    attempt*
    beyond
    cannot
    corrupt*
    damage*
    dead
    died
    disappear*
    drastic
    enough
    error
    exceed*
    fail*
    fatal
    illegal
    impossible
    increase
    insufficient
    invalid
    kill*
    missing
    overflow*
    stget
    stop
    system
    unable
    unexpected*
    unknown
    violation
    warn
    wrong
    "

rm ${REPORT_NAME}

for words in $LIST
do
    RESULTS=`grep -i ${words} ${LOGDIR}/${LOG} | grep -v 'exceeded. Automatically increasing from'`
    if [[ ${RESULTS} > "" ]]
    then
        echo -e "\n-----------------------" >> ${REPORT_NAME}
        echo -e "SEARCH PATTERN = ${words}" >> ${REPORT_NAME}
        echo -e "-------------------------" >> ${REPORT_NAME}
        grep -i ${words} ${LOGDIR}/${LOG} >> ${REPORT_NAME}
    fi
done

MESSAGE_HEADER="Please check these possible errors from the /pdb1/pdb.lg log.\n"
MESSAGE_BODY=`cat ${REPORT_NAME}`
MESSAGE_TAIL="\n\nThis email is from ${0}."
MESSAGE="${MESSAGE_HEADER}${MESSAGE_BODY}${MESSAGE_TAIL}"
SUBJECT="ERROR REPORT from ${LOGDIR}/${LOG}"

for USER in ${USERLIST}
do
    echo -e "${MESSAGE}" | mailx -s "${SUBJECT}" ${USER}
done

答案1

for i in messages cron maillog
do 
if [[ $i  == "messages" ]]
then
echo "Its messages log"
#you can use same script which you are using
#mailx -s subject <emailid>
elif [[ $i == "cron" ]]; 
then 
echo "Its cronlog"; 
#you can use same script which you are using
#mailx -s subject <emailid>
elif [[ $i == "maillog" ]]
then 
echo "its mail log";
#you can use same script which you are using
#mailx -s subject <emailid>
fi
done

相关内容