ksh perl脚本。。如果条件
朋友们 我有一个bash脚本,它每次调用perl脚本并通过电子邮件发送日志文件结果 我想更改我的bash脚本,使其仅在perl子例程行计数器(rcounter++)中有值时才发送电子邮件,而不是一直发送电子邮件 有关于如何更改.ksh文件的提示吗ksh perl脚本。。如果条件,perl,ksh,Perl,Ksh,朋友们 我有一个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_fi
.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
========================================================================
.ksh
#!/bin/ksh
d=`date+%Y%m%d`
log_dir=$HOME
输出文件=log.list
如果!列表表-login/@testdb-outputFile$output\u文件
然后
mailx-s“列表报告:$d”test@mail<$output\u文件
fi
======如果条件对我也适用,请参见下文=============================
列表表-login/@testdb-outputFile$output\u文件
如果[“$?”-ne“0”];
然后
mailx-s“列表报告:$d”test@mail<$output\u文件
fi
========================================================================
Perl脚本:列出表
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...");
}
使用严格;
使用Getopt::Long;
使用DBI;
使用DBD::oracleqw(:ora_类型);
我的$exitStatus=0;
我的%options=()
我的$oracleLogin;
我的$outputFile;
我的$runDate;
我的$logFile;
我的$r计数器;
($oracleLogin,$outputFile)=&validateCommandLine();
my$db=&attemptconnect($oracleLogin);
&reportListTables($outputFile);
$db->$disconnect;
退出($r计数器);
#---------------------------
子报表列表表{
my$outputFile=shift;
如果(!open(OUT,“>”$outputfile)){
&日志消息(“打开$outputFile时出错”);
}
打印并打印;
my$oldDB=“DEFAULT”;
我的$dbcounter=0;
我的$i;
打印并打印标题();
#迭代结果
对于(my$i=0;$iprepare($SQL)或die$db->errstr;
$sth->execute()或die$db->errstr;;
while(my@row=$sth->fethcrow\u数组){
$lstSessions[$rcounter]{owner}=$row[0];
$lstSessions[$rcounter]{object_name}=$row[1];
$lstSessions[$rcounter]{created}=$row[2];
&日志消息(“所有者:$lstSessions[$rcounter]{Owner}”);
&logMessage(“表:$lstSessions[$rcounter]{object_name}”);
&日志消息(“创建:$lstSessions[$rcounter]{created}”);
$rcounter++;
}
&日志消息(“$rcounter记录已找到…”);
}
谢谢
我也很高兴在perl中包含mail-x部分,如果这能让生活变得更简单的话。我不确定我是否正确理解了您的问题。另外,您的代码不完整。因此需要一些猜测
不能从调用方检查本地Perl变量的值 但是如果您的问题是Perl代码是否向日志文件添加了任何内容,那么解决方案很简单:删除“rcounter records found…”行(不管查询是否返回结果,这都没有意义,因为它总是被执行)。然后,让shell脚本在调用Perl之前备份日志文件,然后进行
diff
,仅当diff
告诉您日志文件中添加了输出时才发送邮件
如果这对你没有帮助,请澄清问题
编辑(来自以下评论):
Shell脚本编写并没有那么困难。现在,您的Perl脚本以以下内容结束:
$db->($exitStatus);
这是您的退出代码。无论如何,您都不会在shell脚本中检查它,因此您可以将其更改为更有用的内容,例如写入的数据行数。一个基本的解决方案是通过在Perl脚本的顶部声明$rcounter
全局(而不是本地到getListTables()
)(例如,my$logFile;
之后)。然后您可以将上面的“exitStatus”行替换为:
$rcounter;
瞧,您的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
if list_tables-login/@testdb-outputFile$output_file
然后
mailx-s“列表报告:$d”test@mail<$output\u文件
fi
一个侧面的评论:在我看来,如果你的编程技巧与你试图解决的问题的范围不相称。如果从Perl返回BASH的值会给你带来很多麻烦,你可能需要花时间在教程上,而不是从数据库中获取输入并发送电子邮件。您尝试飞行…
您忘记了插入变量$SQL
和$rcounter
(而是使用了空字SQL
和字符串rcounter
),您声明了$rounter
,但使用了$rcounter
。使用全局变量@lstSessions
很可能是一个非常糟糕的主意。使用使用strict;使用warnings;
,并修复产生的警告/错误。感谢TLP的评论…实际上我在原始代码中使用了$SQL、$rcounter、strict和warning…这因为我没有复制粘贴代码,所以这里的是错别字…如果记录计数器中有值,您知道如何仅通过.ksh发送电子邮件吗?我假定list\u tables
是您的Perl脚本的名称,但您实际上并没有这样说。为什么不将所有逻辑放到Perl脚本中呢?如果其他地方使用了Perl脚本,也许您可以添加一个选项这告诉它适当地调用mailx
。完成按建议放置所有逻辑和文件名…谢谢…是的,我试图从子例程中检查变量rcounter,您说不能从调用者中检查它…基本上我的输出文件从3-4个不同的子例程(页眉、页脚)获取内容因此,如果创建了我从$SQL查询的任何表,请尝试通过电子邮件发送输出文件。@Khallas301:我仍然不确定我是否理解。cough English cough…您可以使用适当的返回代码(send mail/don't send mail)退出Perl脚本,您可以在shell脚本(变量$?
)中签入该代码。如果对于