Perl批处理文件
我有一个Perl脚本,它分割日志文件并向数据库发送查询,我想从windows任务调度器中的bat文件执行这个Perl脚本。它只执行第一个查询,我想执行所有查询 My logfile.txt:Perl批处理文件,perl,batch-file,Perl,Batch File,我有一个Perl脚本,它分割日志文件并向数据库发送查询,我想从windows任务调度器中的bat文件执行这个Perl脚本。它只执行第一个查询,我想执行所有查询 My logfile.txt: Wed Oct 17 04:57:08 2018 : Resource = 'toto' cstep= 'fifi' time =23.634s Wed Oct 17 04:57:50 2018 : Resource = 'titi' cstep= 'fofo' time =22.355s 我的Per
Wed Oct 17 04:57:08 2018 : Resource = 'toto' cstep= 'fifi' time =23.634s
Wed Oct 17 04:57:50 2018 : Resource = 'titi' cstep= 'fofo' time =22.355s
我的Perl脚本
use DBI; use Sys::Hostname ();
$hostname = Sys::Hostname::hostname();
$hostname_cstep_table = $hostname . '_cstep_table';
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=DB;host=IP", "nameDB", 'psswordDB', {'RaiseError' => 1});
#on peut utiliser insert mais il y aura des doublons et ici on est dans une table unique
my $sth = $dbh->prepare("SHOW TABLES LIKE '$hostname_cstep_table';")
or die "prepare statement failed: $dbh->errstr()";
$sth->execute() or die "execution failed: $dbh->errstr()";
#affiche 0 ou 1, selon la table qui est trouvé
$row = $sth->rows;
if ($row eq 1) {
print $hostname_cstep_table . " has created";
}
if ($row eq 0) {
#on peut utiliser insert mais il y aura des doublons et ici on est dans une table unique avec une clé primaire et un ID incrémenté
my $sth = $dbh->prepare(" CREATE TABLE `$hostname_cstep_table` ( `ID` TINYINT ( 3 ) UNSIGNED NOT NULL AUTO_INCREMENT , `time` datetime NOT NULL, `cstep` nvarchar(100)NOT NULL, `time_in_seconde` int NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY (`time`) ) ENGINE=Aria;")
or die "prepare statement failed: $dbh->errstr()";
$sth->execute() or die "execution failed: $dbh->errstr()";
open (FILE, 'logfile');
while (<FILE>) {
($word1, $word2, $word3, $word4, $word5, $word6, $word7, $word8, $word9, $word10, $word11, $word12, $word13, $word14) = split(" ");
$word13 =~ s/[^\d.]//g;
if ($word13 > 5) {
if ($word2 eq "Jan") {
$word2 = "01"
}
if ($word2 eq "Feb") {
$word2 = "02"
}
if ($word2 eq "Mar") {
$word2 = "03"
}
if ($word2 eq "Apr") {
$word2 = "04"
}
if ($word2 eq "May") {
$word2 = "05"
}
print "'$word5-$word2-$word3 $word4', $word11, $word13 \n";
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=DB;host=IP", "nameDB", 'passwordDB',
{'RaiseError' => 1}) ;
#on peut utiliser insert mais il y aura des doublons et ici on est dans une table unique
my $sth = $dbh->prepare("REPLACE `$hostname_cstep_table` (time, cstep, time_in_seconde) VALUES('$word5-$word2-$word3 $word4', $word11, $word13);")
or die "prepare statement failed: $dbh->errstr()";
$sth->execute() or die "execution failed: $dbh->errstr()";
print $sth->rows . " rows found.\n";
$sth->finish;
}
}
}
谢谢你的回答。你的问题真的不是很清楚,但我认为(再次)你把自己和程序中的逻辑搞混了。转换为伪代码后,您的程序如下所示:
connect to the database
check for the existence of the table
if the table exists
say that the table exists
else
create the table
open the log file
for each record in the log file
parse the record
insert record into the table
end for
end if
connect to the database
check for the existence of the table
if the table exists
say that the table exists
else
create the table
end if
open the log file
for each record in the log file
parse the record
insert record into the table
end for
让我们来看看它是如何工作的
第一次运行程序时,我假设表不存在。因此,表被创建,日志文件被处理,许多记录被添加到表中
在程序的每次后续运行中,表都已存在。因此,您的程序只是告诉您该表存在,然后在不处理日志文件的情况下退出
我认为您实际上希望您的程序如下所示:
connect to the database
check for the existence of the table
if the table exists
say that the table exists
else
create the table
open the log file
for each record in the log file
parse the record
insert record into the table
end for
end if
connect to the database
check for the existence of the table
if the table exists
say that the table exists
else
create the table
end if
open the log file
for each record in the log file
parse the record
insert record into the table
end for
我将把它作为一个练习留给您对代码进行实际更改
<强> Update:,请考虑使用其中的一些。我们提出这些建议是有充分理由的:-)
。。问题是什么?我如何使用windows的任务调度器执行perl文件?@jack:你似乎完全忽略了所有给你的好建议。批处理文件中第一行可能的重复必须是调用C:\Users\Desktop\perl\u 32位\portableshell.bat
。因此,上面的read answer解释了从批处理文件中运行或调用批处理文件的所有现有方法。感谢您的回答,但是当我不使用任务计划程序时,只有我的批处理文件有效,但当我使用只创建表而不创建其他查询的批处理文件运行任务计划程序时