> Hi, If someone can help me out please I wrote a perl script which
> opens two text files and then compares them and spit out the
> difference. It is an error checking program which runs on NT
> Platform. The first file has all the required processes listed and
> the second file lists current running processes. The second file is
> being generated at time intervals thru running a bat file
> manually. When you run that batch file it generates the second text
> file which has the current processes running. I want to ask you how
> I can incorporate that batch file in my script so that it can run on
> specified time intervals. After running the bat file the resultant
> text file is going to be my second file in the script. I want my
> script to kick off automatically at predefined time intervals, run
> bat file and then use the text file generated by bat file as the
> second file in the script.
Your check for baseline services is searching the entire array for each
currently running service. The easiest way to check for the
presence/absence of a string value is with a hash. So, put both the
baseline and snapshot service list into hashes, and then list everything in
the baseline hash that's not in the snapshot hash. My example shows a
couple of other useful tricks as well.
use strict;
use warnings;
use FileHandle;
sub read_services
{
my $file = shift;
my $fh = FileHandle->new ($file) or die "can't open $file:
$!\n";
my %hash = map { chomp; ($_,1) } $fh->getlines(); # note (1)
$fh->close();
return %hash;
}
our %baseline = read_services ("baseline.txt"); # note (2)
for ( ; 1; sleep(60) )
{
my %running = read_services ("./snapshot.bat |"); # note (3)
foreach my $missing (grep !$running{$_}, keys %baseline) { #
note (4)
print "$missing is missing\n";
}
}
Notes:
1) You can yield N values within a map() expression; here we build a hash
that maps each line in the service file to the value 1 (true). It's
equivalent to the more prosaic:
my $line;
my %hash;
while ($line = $fh->getline()) {
chomp $line;
$hash{$line} = 1;
}
2) Now %baseline has all the services we expect to see running.
3) Here we exploit Perl's feature that '|' at the end of an opened filename
string can designate the output of a piped command; read_services doesn't
know this is really command output, or even care. If this doesn't work for
you, try
system ("getyoursnapshot.bat");
%running = read_services ("snapshot.txt");
4) This will iterate over every key in the hash %baseline that was not found
in the hash %running -- in other words, your missing services.
Regards,
Jonathan
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]