`

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---";
分享到:
评论

相关推荐

    Unix Shell by Example.zip

    《Unix Shell by Example》这本书是Unix/Linux系统用户和管理员的重要参考资料,它深入浅出地介绍了Unix Shell的使用方法。Shell是Unix系统中的命令解释器,是用户与操作系统交互的桥梁,通过Shell脚本,我们可以...

    Prentice.Hall.PTR.UNIX.Shells.by.Example.4th.Edition

    UNIX Shells by Example is the world’s #1 shell programming book, from the world’s #1 shell programming instructor: Ellie Quigley. In UNIX Shells by Example, Fourth Edition, Quigley has thoroughly ...

    Que.Korn.Shell.Programming.by.Example.Feb.2001

    ### Korn Shell Programming by Example (Feb. 2001) #### 介绍 《Korn Shell Programming by Example》是一本由 Dennis O’Brien 编写的书籍,出版于2001年2月。本书旨在通过实际例子教授读者如何使用 Korn Shell...

    Prentice.Hall.PTR.UNIX.Shells.by.Example.4th.Edition.Sep.2004.eBook-DDU.rar

    《Prentice.Hall.PTR.UNIX.Shells.by.Example.4th.Edition.Sep.2004.eBook-DDU》是一部关于UNIX shell编程的经典教程,由第四版出版于2004年。这本书深入浅出地介绍了UNIX系统中的各种shell,包括Bash、C Shell、...

    UNIX.Shells.by.Example.4th.Edition

    UNIX Shells by Example is the world's #1 shell programming book, from the world's #1 shell programming instructor: Ellie Quigley. In UNIX Shells by Example, Fourth Edition, Quigley has thoroughly ...

    UNIX.Shells.by.Example.3rd.Edition

    该书名为《UNIX.Shells.by.Example.3rd.Edition》,表明这是一本关于UNIX外壳(shell)编程的书籍,而且是第三版。这意味着它在前两版的基础上进行了更新和完善,可能包含了更多的实践案例、最新的技术和改进之处。 ...

    windows下shell命令大全

    - **Example**: `rundll32.exe mshtml.dll,PrintHTML "C:\example.html"` - **Command**: `rundll32.exe shell32.dll,Control_RunDLL mlcfg32.cpl` - **Description**: 打开 Microsoft Exchange 设置。 - **Example...

    UNIX.Shells.by.Example.4th.Edition.en

    文件"Prentice.Hall.PTR.UNIX.Shells.by.Example.4th.Edition.Sep.2004.eBook-DDU.chm"是这本书的电子版,可能采用CHM(Compiled HTML Help)格式,这种格式通常用于技术文档,方便读者快速查找和阅读内容。...

    一些实用的linux命令集,可以方便地实现一些通用功能_linux-shell-example.zip

    一些实用的linux命令集,可以方便地实现一些通用功能_linux-shell-example

    linux shell example例子

    从给定的Linux Shell脚本示例中,我们可以提炼出一系列关键知识点,这些知识点涵盖了Shell编程的基本结构、流程控制、变量处理以及系统操作等方面。下面将详细解释这些知识点: ### 知识点一:基本输入输出与循环...

    创建一个目录,然后在该目录中创建一个名为example.txt的文件,并向其中写入一些文本内容

    创建一个目录,然后在该目录中创建一个名为example.txt的文件,并向其中写入一些文本内容。 执行脚本后,它将检查名为my_new_directory的目录是否存在,如果不存在则创建它,并在该目录中创建一个名为example.txt的...

    如何使用 SHC 加密 Shell 脚本.doc

    `shc -e 01/02/2021 -m "Please contact admin@example.com!" -v -f welcome.sh` 这样,如果有人尝试执行该脚本文件,将显示自定义的到期消息。 SHC 是一个功能强大且实用的工具,用于加密 Shell 脚本并保护敏感...

    短信验证码注册登录Shell - Httpie.sh、Shell - wget.sh文件.zip

    这里的`api.example.com/sms/code`是API接口地址,`phone_number`是参数名,`13800138000`是实际的手机号码。脚本会处理这个请求,获取返回的验证码,并可能将其存储在环境变量或文件中,以便后续使用。 接下来,...

    SSH Secure Shell.rar

    ssh -L 8080:www.example.com:80 user@remotehost ``` 这样,本地的8080端口将转发到远程主机的80端口。 2. **X11转发**:SSH可以将远程主机的图形界面转发到本地,只需开启X11转发并使用`-Y`或`-X`选项。 3. ...

    Android代码-AndroidShell

    Android Shell Execute shell commands on Android. Download the latest AAR or grab via Gradle: compile '...Example of running a command as root: CommandResult result = Shel

    linux下程序设计之shell编程.docx

    - **示例**: `ping www.example.com` 或 `ping 192.168.1.1` - **参数**: - `-c`: 指定发送Echo Request的次数。 - `-i`: 设置两次请求之间的间隔时间。 - **ifconfig命令**: - **作用**: 用于配置网络接口的...

    Unix shell by Example

    《Unix Shell by Example》是一本深入讲解Unix Shell的权威书籍,它为学习和理解Unix操作系统提供了丰富的实践案例。这本书详细阐述了如何利用Shell这个强大的命令行界面进行日常操作,包括文件管理、脚本编写、系统...

Global site tag (gtag.js) - Google Analytics