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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNIX to perl Help

Status
Not open for further replies.

DanNJ

MIS
Mar 19, 2003
29
0
0
US
I want to port some unix scripts to perl, mostly for the fun of it. If anyone is kind enough could you help me convert this stuff. Converting these two functions should give me enough of a feel for it to do the rest.


#!/bin/sh
##################
# USAGE_function #
##################
USAGE_function (){
if [ $1 != 1 ]
then
echo "usage: $1 date"
exit 1
fi
}
######################
# RUNREPORT_function #
######################
RUNREPORT_function (){
YEST=`nawk 'BEGIN { FS = &quot;,&quot; } ; $1 == '&quot;$DATE2&quot;' && $3 == &quot;ERROR&quot; { print $5, &quot;\n&quot; }'< $REPORT_LOG`
if [ $YEST ]
then
/use/bin/echo &quot;$YEST&quot; > /tmp/fast$$
/use/bin/echo &quot;Hit RETURN to exit&quot; >> /tmp/fast$$
(
/usr/openwin/bin/cmdtool -Wp 550 450 -Ws 600 300 -fg black -bg red -font r14 -label &quot; Disk Cleanup Error &quot; &quot;$scripts/rep.sh&quot; &quot;/tmp/fast$$&quot;
/usr/bin/rm /tmp/fast$$
)
fi
}

DATE2=$1
RUNREPORT_function
USAGE_function $#
Exit 0
 
Hello Dan,

I'm not an NAWK or AWK person (AWKward and Nasty and AWKward if you ask me :)) so could you tell us what the NAWK bit does? We can go from there.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
nawk 'BEGIN { FS = &quot;,&quot; }
nawk 'At the BEGINing set the Field Seperator to &quot;,&quot;

$1 == '&quot;$DATE2&quot;'
check if the first field is equal to &quot;$DATE2&quot;

&& $3 == &quot;ERROR&quot;
Also check is the third field is equal to &quot;ERROR&quot;

{ print $5, &quot;\n&quot; }'< $REPORT_LOG
If both field 1 and 3 check out then print field 5 with an end of line.

< $REPORT_LOG
Use the report log as input


Thanks
 
ok - so what's format of report log?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
DATE,TIME,ERROR,EXIT CODE,ERROR MSG

example:
20031002,13:01:01,ERROR,1,Error copying file xyz
 
ok, so

use strict;
use warnings;

# first - get date from command line
my $date2 = shift or die &quot;usage $0 date\n&quot;;
# and then declare some variables
my $reportlog = 'report.log';
my $tmp = &quot;/tmp/fast.$$&quot;;
my $cmd = &quot;/usr/openwin/bin/cmdtool -Wp 550 450 -Ws 600 300 -fg black -bg red -font r14 -label \&quot; Disk Cleanup Error \&quot;&quot;;
my ($DATE,$TIME,$ERROR,$EXITCODE,$ERRORMSG);

# now open report log
open (F, $reportlog )
or die &quot;Can't open file $reportlog\n$!\n&quot;;
# and process each line
while(<F>){
# first - get rid of end-of-line character
chomp;
# and cut input line up into fields
($DATE,$TIME,$ERROR,$EXITCODE,$ERRORMSG) = split(/,/);
# don't care unless it's an error
next unless $ERROR eq 'ERROR';
# or if it doesn't match the date you want
next unless $DATE eq $DATE2;

# if it get's here we will be calling cmdtool
# create temporary file
open(TMP, &quot;>$tmp&quot;)
or die &quot;Can't create file $tmp\n$!\n&quot;;
print TMP &quot;$ERRORMSG\n&quot;;
print TMP &quot;Hit RETURN to exit\n&quot;;
close TMP;
# and then call cmdtool
system(&quot;$cmd $scripts/rep.sh $tmp&quot;);
# and delete temporary file
unlink($tmp);
}

The (untested, sorry) code above processes each line in report log and calls cmdtool for each line that matches the date and contains the word ERROR in the third field.

I hope this gives you a start, and a flavour of how Perl can be used.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Thx very much, I have a lot of questions but I will see if I can figure it out on my own first. Thanks for the jump start.
 
no problem dan, jump start was the intention, come back with problems if you have them

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top