Script Not Returning Value

D

Digger

#!/usr/bin/perl -w
#
$ARGV[0] = 'url2.log';
my %status;
while (<>) {
/ (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
}
print "URLs whose last status was SUCCESS:\n";
$status{$_} eq 'SUCCESS' and print " $_\n" for sort keys %status;

print "\nURLs whose last status was FAILED:\n";
$status{$_} eq 'FAILURE' and print " $_\n" for sort keys %status;
#

1) The above script prints out url's from a log file with the FAILED
status if and ONLY if further down the log file a SUCCESS is not
found. (so www.234.com ans www.abc.com would be echoed out)

IE:
snip from log file....
[2005-01-04 09:17:59] FAILURE RESPONSE www.123.com
[2005-01-04 09:18:59] SUCCESS RESPONSE www.678.com
[2005-01-04 09:19:59] FAILURE RESPONSE www.234.com
[2005-01-04 09:20:59] FAILURE RESPONSE www.abc.com
[2005-01-04 09:23:59] SUCCESS RESPONSE www.123.com

2) The above script prints out url's from a log file with the SUCCESS
status if and ONLY if further down it did not fail.
(so www.123.com www.123.com)

Anyway I would like to modify this program.

1. I would like instead of printing out the url's I would like it to
spit out a 1 if a FAILURE url is found and not cleared up further down
the file. So I am trying to add the following
print LOGFILE "1" if /FAIL/;
but it's not working..... If the log file has more than one FAILURE
it's printing out many 1's.

Thanks
~
 
B

Brian McCauley

Digger said:
Subject: Script Not Returning Value

I can see no connection between this subject line and the body of your post.
#!/usr/bin/perl -w
#
$ARGV[0] = 'url2.log';
my %status;
while (<>) {
/ (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
}
print "URLs whose last status was SUCCESS:\n";
$status{$_} eq 'SUCCESS' and print " $_\n" for sort keys %status;

print "\nURLs whose last status was FAILED:\n";
$status{$_} eq 'FAILURE' and print " $_\n" for sort keys %status;
#

1) The above script prints out url's from a log file with the FAILED
status if and ONLY if further down the log file a SUCCESS is not
found. (so www.234.com ans www.abc.com would be echoed out)

IE:
snip from log file....
[2005-01-04 09:17:59] FAILURE RESPONSE www.123.com
[2005-01-04 09:18:59] SUCCESS RESPONSE www.678.com
[2005-01-04 09:19:59] FAILURE RESPONSE www.234.com
[2005-01-04 09:20:59] FAILURE RESPONSE www.abc.com
[2005-01-04 09:23:59] SUCCESS RESPONSE www.123.com

2) The above script prints out url's from a log file with the SUCCESS
status if and ONLY if further down it did not fail.
(so www.123.com www.123.com)

Anyway I would like to modify this program.

1. I would like instead of printing out the url's I would like it to
spit out a 1 if a FAILURE url is found and not cleared up further down
the file. So I am trying to add the following
print LOGFILE "1" if /FAIL/;
but it's not working..... If the log file has more than one FAILURE
it's printing out many 1's.

The above script did not open a filehandle LOGFILE thus the above line
would do nothing. So you are telling us that you inserted the above
line in a unspecified position in a script you haven't shown as and it
is not doing what you expect. If the line above is printing more then
one '1' then it was probably in a loop. Take it outside the loop.

Please generate a minmal but complete script that illustrates your
problem and post it here.

This and much other valuable information can be found in the posting
guidelines.

Random shot in the dark:

print LOGFILE "1" if grep /FAIL/, values %status;
 
J

Jürgen Exner

Digger wrote:

For a script to return a custom return value you have to use the exit()
function.
I don't see you using it anywhwere in your code.
#!/usr/bin/perl -w
#
$ARGV[0] = 'url2.log';
my %status;
while (<>) {
/ (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
}
print "URLs whose last status was SUCCESS:\n";
$status{$_} eq 'SUCCESS' and print " $_\n" for sort keys %status;

print "\nURLs whose last status was FAILED:\n";
$status{$_} eq 'FAILURE' and print " $_\n" for sort keys %status;
#

jue
 
D

Digger

Digger wrote:

For a script to return a custom return value you have to use the exit()
function.
I don't see you using it anywhwere in your code.
#!/usr/bin/perl -w
#
$ARGV[0] = 'url2.log';
my %status;
while (<>) {
/ (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
}
print "URLs whose last status was SUCCESS:\n";
$status{$_} eq 'SUCCESS' and print " $_\n" for sort keys %status;

print "\nURLs whose last status was FAILED:\n";
$status{$_} eq 'FAILURE' and print " $_\n" for sort keys %status;
#

jue


Here's what I have so far....


#!/usr/bin/perl -w
#
open (SNMPLOG, ">snmp.log");
$ARGV[0] = 'url2.log';
my %status;
while (<>) {
/ (FAILURE|SUCCESS).+?from (.+)/ and $status{$2} = $1;
}
$status{$_} eq 'FAILURE' and print SNMPLOG " 0 " for sort keys
%status;

$status{$_} eq 'SUCCESS' and print SNMPLOG " 1 " for sort keys
%status;

The logfile has the following:

1 0 0 0


Is there a way to do the following:

If there is a FAILURE print 0 to the logfile then exit. Or if no url's
FAILURE print the 1 to the log file...... I guess I would have to nest
an if statement in there some how???


$status{$_} eq 'FAILURE' and print SNMPLOG " 0 " for sort keys
%status;

$status{$_} eq 'SUCCESS' and print SNMPLOG " 1 " for sort keys
%status;
 
J

Jürgen Exner

Digger said:
Digger wrote:

For a script to return a custom return value you have to use the
exit() function.
I don't see you using it anywhwere in your code.
[First code snipped]

Here's what I have so far....

[updated code snipped]

Ok, very slowly again:
You want your script to return a value.
To define the return value of a script and to terminate it at the same time
you use the exit() function in Perl.
Your updated program still does not call the exit() function anywhere.
Therefore it does not return a value (well, depending on you OS it may
return some default value for success or failure).

jue
 
D

Digger

Digger said:
Digger wrote:

For a script to return a custom return value you have to use the
exit() function.
I don't see you using it anywhwere in your code.
[First code snipped]

Here's what I have so far....

[updated code snipped]

Ok, very slowly again:
You want your script to return a value.
To define the return value of a script and to terminate it at the same time
you use the exit() function in Perl.
Your updated program still does not call the exit() function anywhere.
Therefore it does not return a value (well, depending on you OS it may
return some default value for success or failure).

jue
So how and where do I put it???
 
T

Tad McClellan

Digger said:
open (SNMPLOG, ">snmp.log");


You should always, yes *always*, check the return value from open():

open (SNMPLOG, ">snmp.log") or die "could not open 'snmp.log' $!";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top