...好友
我有bash脚本,每次调用perl脚本和电子邮件日志文件结果。
我想更改我的bash脚本,以便只有在perl子例程行计数器(rcounter ++)中有值时才发送电子邮件,而不是所有时间。
有关如何更改.ksh文件的任何提示?
.ksh
#!/bin/ksh
d=`date +%Y%m%d`
log_dir=$HOME
output_file=log.list
if ! list_tables -login /@testdb -outputFile $output_file
then
mailx -s "list report : $d" test@mail < $output_file
fi
=======Below if condition also works for me=============================
list_tables -login /@testdb -outputFile $output_file
if ["$?" -ne "0"];
then
mailx -s "list report : $d" test@mail < $output_file
fi
========================================================================
Perl脚本:list_tables
use strict;
use Getopt::Long;
use DBI;
use DBD::Oracle qw(:ora_types);
my $exitStatus = 0;
my %options = ()
my $oracleLogin;
my $outputFile;
my $runDate;
my $logFile;
my $rcounter;
($oracleLogin, $outputFile) = &validateCommandLine();
my $db = &attemptconnect($oracleLogin);
&reportListTables($outputFile);
$db->$disconnect;
exit($rcounter);
#---------------------------
sub reportListTables {
my $outputFile = shift;
if ( ! open (OUT,">" . $outputfile)) {
&logMessage("Error opening $outputFile");
}
print OUT &putTitle;
my $oldDB="DEFAULT";
my $dbcounter = 0;
my $i;
print OUT &putHeader();
#iterate over results
for (my $i=0; $i<=$lstSessions; $i++) {
# print result row
print OUT &putRow($i);
$dbCounter++;
}
print OUT &putFooter($dbCounter);
print OUT " *** Report End \n";
closeOUT;
}
#------------------------------
sub putTitle {
my $title = qq{
List Tables: Yesterday
--------------
};
#------------------------------
sub putHeader {
my $header = qq{
TESTDB
==============
OWNER Table Created
};
#------------------------------
sub putRow {
my $indx = shift;
my $ln = sprintf "%-19s %-30s %-19s",
$lstSessions[$indx]{owner},
$lstSessions[$indx]{object_name},
$lstSessions[$indx]{created};
return "$ln\n";
}
#------------------------------
sub getListTables {
my $runDt = shift;
$rcounter = 0;
my $SQL = qq{
selct owner, object_name, to_char(created,'MM-DD-YYYY') from dba_objects
};
my $sth = $db->prepare ($SQL) or die $db->errstr;
$sth->execute() or die $db->errstr;;
while (my @row = $sth->fethcrow_array) {
$lstSessions[$rcounter] {owner} =$row[0];
$lstSessions[$rcounter] {object_name} =$row[1];
$lstSessions[$rcounter] {created} =$row[2];
&logMessage(" Owner: $lstSessions[$rcounter]{owner}");
&logMessage(" Table: $lstSessions[$rcounter]{object_name}");
&logMessage(" created: $lstSessions[$rcounter]{created}");
$rcounter++;
}
&logMessage("$rcounter records found...");
}
感谢..
也很高兴在perl中包含mail-x部分,如果这样可以让生活更轻松..
答案 0 :(得分:0)
我不确定我是否理解你的问题。此外,您的代码不完整。所以有一些猜测。
你无法检查来自呼叫方的本地Perl变量的值。
但是,如果您的问题是Perl代码是否在日志文件中添加了任何内容,那么解决方案很简单:删除“找到的rcounter records ...”行(无论如何都没有意义,因为总是< / em>执行,查询是否返回结果)。然后,让shell脚本在调用Perl之前备份日志文件,之后再生成diff
,仅在diff
告诉您已将输出添加到日志文件时发送邮件。
如果这对您没有帮助,请澄清问题。
编辑(来自以下评论):
Shell脚本编写并不困难。现在,您的Perl脚本以:
结束$db->($exitStatus);
这是你的退出代码。您无论如何都不会在shell脚本中检查它,因此您可以将其更改为更有用的内容,例如写入的数据行数。一个原始的解决方案是通过在Perl脚本的顶部声明它(例如在$rcounter
之后)使getListTables()
全局(而不是本地到my $logFile;
)。然后你可以简单地替换上面的“exitStatus”行:
$rcounter;
Voila,您的Perl脚本现在返回写入的数据行数。
在Perl中,返回码0被视为失败,任何其他值都是成功的。在shell中,它是另一种方式 - 但幸运的是,您不必担心这一点,因为Perl知道这一点并且在返回到调用shell时“反转”(否定)脚本的返回代码。
所以你需要的只是让邮件依赖于Perl的非零返回:
if list_tables -login /@testdb -outputFile $output_file
then
mailx -s "list report : $d" test@mail < $output_file
fi
侧面评论:我认为您的编程技巧与您尝试解决的问题范围不相符。如果从Perl返回一个值到bash会给你带来很多麻烦,你应该花时间学习教程,而不是从数据库获取输入并发送电子邮件。在你试飞之前学会走路......