John W. Krahn wrote:
Jas wrote:

I am still new to working with Perl myself but I think I know the anwer
to this one...

my $cmd = system('netstat -s')or die "Could not run command: $1";
my $cmd = system('netstat -a | grep tcp')or die "Could not run command: $1";


If netstat executed successfully then it will return 0 (zero) otherwise
it will return a non-zero value if an error occurred.  system() returns
that value so $cmd will be a numerical value and if it is zero 'or' will
cause die() to execute which means that if netstat was successful the
program will end and if netstat failed the program will continue.  Also
you have $1 in the error message which is only relevant to regular
expressions.



The answer would be to detemine WHAT an error or warning is during program execution -

# generally OK, I guess I am guilty of this -
$erc = system('netstat -a | grep tcp') / 256 unless $erc;
warn "An error occurred from the last chained command..." if $erc;


Better would be -


#!/usr/bin/perl -w

my $VERSION = 'xyz';
my $AUTHOR  = 'Sx';

my $stuff = $ARGV[0] || 'help';
&help() unless ($stuff =~ /\w\.(.+){2,4}/);

&print_if_Warn if (system("nstat -a") / 256);

&print_if_Fatal if (system("netstat -nr") / 256);


################### Display General Help... sub help() { print<<_XHELP_;

Program: $0\tVersion: $VERSION\nAuthor: $AUTHOR

FATAL: Usage: [perl] $0 pattern.in.form

... some long winded explaination of:

/ pattern match /

_XHELP_

exit;
};


################### Display WARN... sub print_if_Warn() { print<<_XHELP_;

Warning: Unexpected things happened but we are OK...

Continuing...

_XHELP_

sleep 5;
};


################### Display FATAL... sub print_if_Fatal() { print<<_XHELP_;

FATAL: I fall down go boom...

Application exiting...

_XHELP_

exit;
};

__END__


Output: bash-2.05$ perl sxWARN text.in.here Can't exec "nstat": No such file or directory at sxWARN line 9.

Program: sxWARN Version: xyz
Author: Sx

Warning: Unexpected things happened but we are OK...

Continuing...


Routing Table: IPv4 Destination Gateway Flags Ref Use Interface -------------------- -------------------- ----- ----- ------ --------- 192.168.1.0 192.168.1.69 U 1 47 hme0 224.0.0.0 192.168.1.69 U 1 0 hme0 default 192.168.1.1 UG 1 630 127.0.0.1 127.0.0.1 UH 17 789 lo0



PS - you execute this example like this:
perl progname pattern.in.text

-Bill-
__Sx__________________________________________
http://youve-reached-the.endoftheinternet.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to