Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > system problem

Reply
Thread Tools

system problem

 
 
Chip
Guest
Posts: n/a
 
      05-16-2005
Thanks in advance for your help.

I am trying to fill an array by using grep
so I can print out only the lines with ip
addresses in them.

The following prints the system call on
the screen but does not fill the array.

I don't want the system call to print on
the screen, and I want to fill the array.

#!/usr/bin/perl -w
use strict;
my @array = system("ps ax | grep sendmail");
foreach (@array) {
if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
print "$_ \n";
}
}

Any and all help is appreciated.

Chip


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-16-2005
Chip wrote:
> The following prints the system call on
> the screen but does not fill the array.
>
> I don't want the system call to print on
> the screen, and I want to fill the array.
>
> #!/usr/bin/perl -w
> use strict;
> my @array = system("ps ax | grep sendmail");


Then don't use system(). The docs explain why and what to use instead:

perldoc -f system

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-16-2005
"Chip" <(E-Mail Removed)> wrote in news:(E-Mail Removed):

> The following prints the system call on
> the screen but does not fill the array.
>
> I don't want the system call to print on
> the screen, and I want to fill the array.


Then you should read the documentation:

perldoc -f system

> #!/usr/bin/perl -w


use warnings;

is preferable. Read more about it in

perldoc warnings
perldoc perllexwarn

> use strict;
> my @array = system("ps ax | grep sendmail");


Once you have read about the backtick operator (see above),
you might still want to avoid spawning the grep above, and
instead use a pipe open to ps, and process the output of the
command line by line. See:

perldoc -f open

> foreach (@array) {
> if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {


That would match things other than IP addresses as well.

See

<URL: http://search.cpan.org/dist/Regexp-Common/lib/Regexp/Common/net.pm>

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Chip
Guest
Posts: n/a
 
      05-17-2005
Thanks for your help.

#!/usr/bin/perl
use strict;
use warnings;
my $ps = `ps ax | grep sendmail`;
my @array = split /\n/, $ps;
foreach (@array) {
if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
print "$_ \n";
}
}

Seems to work ok for me.
Chip

"A. Sinan Unur" <(E-Mail Removed)> wrote in message
news:Xns9658C88D4120Dasu1cornelledu@127.0.0.1...
> "Chip" <(E-Mail Removed)> wrote in

news:(E-Mail Removed):
>
> > The following prints the system call on
> > the screen but does not fill the array.
> >
> > I don't want the system call to print on
> > the screen, and I want to fill the array.

>
> Then you should read the documentation:
>
> perldoc -f system
>
> > #!/usr/bin/perl -w

>
> use warnings;
>
> is preferable. Read more about it in
>
> perldoc warnings
> perldoc perllexwarn
>
> > use strict;
> > my @array = system("ps ax | grep sendmail");

>
> Once you have read about the backtick operator (see above),
> you might still want to avoid spawning the grep above, and
> instead use a pipe open to ps, and process the output of the
> command line by line. See:
>
> perldoc -f open
>
> > foreach (@array) {
> > if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {

>
> That would match things other than IP addresses as well.
>
> See
>
> <URL: http://search.cpan.org/dist/Regexp-Common/lib/Regexp/Common/net.pm>
>
> Sinan
>
> --
> A. Sinan Unur <(E-Mail Removed)>
> (reverse each component and remove .invalid for email address)
>
> comp.lang.perl.misc guidelines on the WWW:
> http://mail.augustmail.com/~tadmc/cl...uidelines.html



 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-17-2005
Chip wrote:

> my $ps = `ps ax | grep sendmail`;
> my @array = split /\n/, $ps;


Change that to either

my @array = `ps ax | grep sendmail`;

or

my @array = grep /sendmail/,`ps ax`;

-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with difference between System.Web.Mail and System.Net.Mail in using attachments Brad ASP .Net 2 04-25-2006 07:58 PM
System.Security.SecurityException: Error de solicitud de permiso de tipo System.Net.WebPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. Luis Esteban Valencia ASP .Net 0 07-14-2005 01:43 PM
Streaming with System.IO and System.Net Charles A. Lackman ASP .Net 4 01-15-2005 08:00 PM
System error 203 The System could not find environment option sfunds sfunds ASP .Net 9 05-06-2004 09:46 AM
Method not found: System.Collections.Specialized.NameValueCollection System.Web.HttpRequest.get_QueryString(). Mark Miller ASP .Net 1 07-18-2003 08:48 PM



Advertisments