LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-30-2009, 12:32 PM   #1
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Rep: Reputation: 0
doing in perl ----> ps -ef | grep "/bin/sh /opt"


i need to grep the output of the ps command and this is what i have to do

So if this returns a non-null result set, i continue processing otherwise exit or break from loop...

ps -ef | grep "/bin/sh /opt"

what would the syntax for perl be...
 
Old 09-30-2009, 01:35 PM   #2
mmrtnt
LQ Newbie
 
Registered: Nov 2006
Location: las vegas, nevada
Distribution: fedora core x
Posts: 25

Rep: Reputation: 7
if( `ps -ef | grep "/bin/sh /opt"` )
{
print "Found the proc\n";
}
else
{
print "Proc not found\n";
}

-- or --

if( `ps -ef | grep -c "/bin/sh /opt"` > 0 )
{...}
else
{...}

-- or --

$test = `ps -ef | grep "/bin/sh /opt"`;
if( defined( $test ) )
{...}
else
{...}


Have you looked at pgrep?
 
Old 09-30-2009, 02:07 PM   #3
Prehistorik
LQ Newbie
 
Registered: Oct 2006
Location: St. Petersburg, Russia
Distribution: Fedora
Posts: 17

Rep: Reputation: 1
Do you want to replace the command with Perl script completely?
Or do you just want to get rid of grep?

A script for the second case:

Code:
#!/usr/bin/perl
use strict;

my $CMD = '/bin/sh /opt';
my $C = qr/$CMD/;

my $run = 1;
while($run) {
  open I, "ps -ef |" || die "Cannot open pipe";
  $_ = <I>;
  last unless scalar (grep { /$C/ } map { /^(?:\S+\s+){7}(.*)$/; $1 } <I>);
  print localtime(time) . ": Still running...\n";
  close I;
}
(UPDATE)
...but the script above by mmrtnt does the same in a much simpler way...

Last edited by Prehistorik; 09-30-2009 at 02:10 PM. Reason: a bit too late :)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
slackware 13.0 32bit the "/usr/bin/sha1pass" perl script can NOT run ginkgo Slackware 3 03-05-2010 05:00 AM
Regarding # /opt/CTEact/bin/act & /opt/SUNWexplo/bin/explorer not available for x86 rajaniyer123 Solaris / OpenSolaris 6 04-24-2008 09:09 AM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 04:51 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM


All times are GMT -5. The time now is 01:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration