shell example.

#!/bin/sh
#=============================================================================
# Author : Gareth Liao
# Desc : runMonitorInquireIncrease.sh
# Run: sh runMonitorInquireIncrease.sh Inquiry
#=============================================================================

export SNARK_HOME=$HOME/abc

curParam=$1;
curTable='';
oldFileName=$HOME/inquire_increase_old.txt;
newFileName=$HOME/inquire_increase_new.txt;
lastTimeFile=$HOME/inquire_time.txt;
export SQL_SCRIPT_HOME=$SNARK_HOME/src/sql;
CTRL_FILE=$SNARK_HOME/data/import_cache/.ImportCtrl;

echo "--- start of runMonitorInquireIncrease.sh---";


#RCP='garethliao@**.com';

EMAIL_FROM='pscadmin@**.com';

userpwd=$2;
sid=$3;

#userpwd='test/test';
#sid='test';

nowTime=$(date +'%Y-%m-%d %H:%M:%S');
today=$(date +%Y-%m-%d);
yesterday=$(date -d yesterday +%Y-%m-%d);
beforeYesterday=$(date -d '2 days ago' +%Y-%m-%d);

lastRunTime='';

todayTotal=0;
yesterdayTotal=0;
beforeYesterdayTotal=0;


newtodayTotal=0;
newyesterdayTotal=0;
newbeforeYesterdayTotal=0;

todayAdd=0;
yesterdayAdd=0;
beforeyesterdayAdd=0;
totalAdd=0;

function sendEmail
{
todayAdd=`expr $newtodayTotal - $todayTotal`;
if [ $todayAdd -lt 0 ]
then todayAdd=0;
fi;
yesterdayAdd=`expr $newyesterdayTotal - $yesterdayTotal`;
if [ $yesterdayAdd -lt 0 ]
then yesterdayAdd=0;
fi;

beforeyesterdayAdd=`expr $newbeforeYesterdayTotal - $beforeYesterdayTotal`;
if [ $beforeyesterdayAdd -lt 0 ]
then beforeyesterdayAdd=0;
fi;

totalAdd=`expr $todayAdd + $yesterdayAdd`;
totalAdd=`expr $totalAdd + $beforeyesterdayAdd`;


if [ $totalAdd -gt 0 ]
then
send_TMP=".tmp";

if [ -f $zipfile ]
then
echo "Prepare to send....to $RCP";
cat >> $send_TMP << EOT
Subject : Monitor Report of
Content-Type:text/plain
From: $EMAIL_FROM
To: $RCP


Dear All,

The following is the result about monitor data(last running is $lastRunTime).
$today last time is $todayTotal current is $newtodayTotal increased number is $todayAdd
$yesterday last time is $yesterdayTotal current is $newyesterdayTotal increased number is $yesterdayAdd
$beforeYesterday last time is $beforeYesterdayTotal current is $newbeforeYesterdayTotal increased number is $beforeyesterdayAdd

Regards,
Admin

EOT

uuencode $zipfile $zipfile >> $send_TMP;
cat $send_TMP | /usr/sbin/sendmail -f $EMAIL_FROM $RCP;
rm $send_TMP;
fi;

fi;


if [ "$totalAdd" = "0" ]
then
send_TMP=".tmp";

if [ -f $zipfile ]
then
echo "Prepare to send....to $RCP";
cat >> $send_TMP << EOT
Subject : No data added, Monitor Report of the PSE inquire increase for $curParam
Content-Type:text/plain
From: $EMAIL_FROM
To: $RCP


Dear All,

No datas added by now.

The following is the result about monitor data (last running is $lastRunTime).

Regards,
Admin

EOT

uuencode $zipfile $zipfile >> $send_TMP;
cat $send_TMP | /usr/sbin/sendmail -f $EMAIL_FROM $RCP;
rm $send_TMP;
fi;

fi;

}

function sendAlertEmail
{
send_TMP=".tmp";
if [ -f $zipfile ]
then
echo "Prepare to send....to $RCP";
cat >> $send_TMP << EOT
Subject : Alert Email for process dead
Content-Type:text/plain
From: $EMAIL_FROM
To: $RCP

Dear All,

The import process is dead, it was killed now.
It is from the monitor of inquiry.

Regards,
PSC Admin

EOT

uuencode $zipfile $zipfile >> $send_TMP;
cat $send_TMP | /usr/sbin/sendmail -f $EMAIL_FROM $RCP;
rm $send_TMP;
fi
}

function tableName
{

if [ "$curParam" = "Inquiry" ]
then
curTable='export_inquiry_increase.sql';
oldFileName=$SNARK_HOME/i_inquire_increase_old.txt;
newFileName=$SNARK_HOME/i_inquire_increase_new.txt;
elif [ "$curParam" = "InquireAll" ]
then
curTable='export_inquireall_increase.sql';
oldFileName=$SNARK_HOME/ia_inquire_increase_old.txt;
newFileName=$SNARK_HOME/ia_inquire_increase_new.txt;
elif [ "$curParam" = "AggInquiry" ]
then
curTable='export_agginquiry_increase.sql';
oldFileName=$SNARK_HOME/ai_inquire_increase_old.txt;
newFileName=$SNARK_HOME/ai_inquire_increase_new.txt;
elif [ "$curParam" = "AggInquireAll" ]
then
curTable='export_agginquireall_increase.sql';
oldFileName=$SNARK_HOME/aia_inquire_increase_old.txt;
newFileName=$SNARK_HOME/aia_inquire_increase_new.txt;
fi;
}


function exportData
{
echo "Exporting data to text file from database ...";
echo "---- Current table:"$curTable;
#sqlplus -s $userpwd@$sid @$SQL_SCRIPT_HOME/$curTable >> $newFileName;
if [ "$curParam" = "Inquiry" ]
then
sqlplus -s $userpwd@$sid @$SQL_SCRIPT_HOME/export_inquire_increase.sql >> $newFileName;
elif [ "$curParam" = "InquireAll" ]
then
sqlplus -s $userpwd@$sid @$SQL_SCRIPT_HOME/export_inquireall_increase.sql >> $newFileName;
elif [ "$curParam" = "AggInquiry" ]
then
sqlplus -s userpwd@$sid @$SQL_SCRIPT_HOME/export_agginquiry_increase.sql >> $newFileName;
elif [ "$curParam" = "AggInquireAll" ]
then
sqlplus -s $userpwd@$sid @$SQL_SCRIPT_HOME/export_agginquireall_increase.sql >> $newFileName;
fi;

echo ">>>>> END export";
}

function readTime
{
if [ -f $lastTimeFile ]
then
while read line
do
lastRunTime=$line;
done < $lastTimeFile;
fi;
}

function writeTime
{
echo $nowTime > $lastTimeFile;
}

function readFiles
{
if [ -f $oldFileName ]
then
while read line
do
sendDate=`echo $line | awk '{print $1}'`;
total=`echo $line | awk '{print $2}'`;
echo "old file $sendDate --- today is $today";
if [ "$sendDate" = "$today" ]
then
todayTotal=`expr $total + 0`;
echo "1111 $todayTotal";
elif [ "$sendDate" = "$yesterday" ]
then
yesterdayTotal=`expr $total + 0`;
echo "222 $yesterdayTotal";
elif [ "$sendDate" = "$beforeYesterday" ]
then
beforeYesterdayTotal=`expr $total + 0`;
echo "3333 $beforeYesterdayTotal";
fi;
done < $oldFileName;
fi;


while read newline
do
newsendDate=`echo $newline | awk '{print $1}'`;
newtotal=`echo $newline | awk '{print $2}'`;
if [ "$newsendDate" = "$today" ]
then
newtodayTotal=`expr $newtotal + 0`;
elif [ "$newsendDate" = "$yesterday" ]
then
newyesterdayTotal=`expr $newtotal + 0`;
elif [ "$newsendDate" = "$beforeYesterday" ]
then
newbeforeYesterdayTotal=`expr $newtotal + 0`;
fi;
done < $newFileName;

rm -rf $oldFileName;
mv $newFileName $oldFileName;

}

function checkAndKill
{
if [ -f $CTRL_FILE ]
then
modify_time=`ls -l $CTRL_FILE | awk '{print $(NF-1)}'`;
curtime=`date +"%H:%M"`;
sys_time=`date -d "$curtime" +%s`;
file_time=`date -d "$modify_time" +%s`;
interval=`expr $sys_time - $file_time`;
ta=`expr $newtodayTotal - $todayTotal`;
ya=`expr $newyesterdayTotal - $yesterdayTotal`;
ba=`expr $newbeforeYesterdayTotal - $beforeYesterdayTotal`;
aa=`expr $ta + $ya + $ba`;
if [ $interval >= 14400 ]
then
if [ $aa <= 0 ]
then
PID=`ps gaux | grep dailyImportInquiry.sh | grep -v grep | awk '{print $2}'`;
kill -9 $PID;
sendAlertEmail;
fi
fi
fi
}

function main
{
#tableName;
readTime;
writeTime;
exportData;
readFiles;
#checkAndKill;
sendEmail;
}

main;
echo "--- end of runMonitorInquireIncrease.sh---";

    推荐阅读