Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert this script to perl

Status
Not open for further replies.

rchilson11

Technical User
Dec 21, 2010
3
US
DT=`echo $1`
NOTIFY=rchilson@hotmail.com
PROD_DIR=/apps*
if [ "$DT" = '' ] || [ "$DT" = ' ' ] ; then
DTPULL=`date -d yesterday '+%Y-%m-%d'`
else
DTPULL=`date --date "$DT" '+%Y-%m-%d'` || exit -1
fi
(
TOT=`dsh -g prod "gzcat /apps*/servlet-timer.log.2010-12-13* | grep 2009" | wc -l`
echo "Transaction Report: $DTPULL $HOSTNAME"
echo ""
echo " 2009............................................:$TOT
echo ""
) | mailx -s "Prod Transaction Stats for: $DTPULL" $NOTIFY
 
I want to learn perl and figured I could learn best by examples. I have been reading tech articles and I would like to know the easiest way to convert this bash script to perl. Thanks!!
 
this is how far I got..

#!/usr/bin/perl
use strict;
use warnings;

# Specify a date
my $date = shift || undef;

#Search string as the script argument
my $2009 = shift || die "No Cardholder2010 found!\n";

# Get the gzipped log files as an array
my @files = glob '/apps/servlet-timer.log*' .
(defined $date ? $date : '') . '.gz';


# Loop on the log files, open and read them, and
# print out any lines with given Search string in them
foreach my $file (@files) {
open FILE, "gunzip -c $file|" or die $!;

while (my $line = <FILE>) {
print $line if index($line, '"' . $2009 . '"') >= 0;
}

close FILE;
}
 
Nicely done, so far as I can see. Does it behave as you expect so far? I particularly like the clear layout of the script, v cool and easy to read.

Two things.

1 - the variable $2009, I don't know if that (a variable name made all of digits) will work or not, change to something starting with a letter if it's a problem, like this: $yr2009

2 - I'd suggest one change which might make it a bit more Perl ish, and it will run a little faster as well.

print $line if $line =~ /$2009/;

So the next step then - to store and email the results to someone?

Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top