Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-30-2009, 01:32 PM   #1
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, 02:35 PM   #2
LQ Newbie
Registered: Nov 2006
Location: las vegas, nevada
Distribution: fedora core x
Posts: 25

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

-- or --

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

-- or --

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

Have you looked at pgrep?
Old 09-30-2009, 03:07 PM   #3
Registered: Oct 2006
Location: St. Petersburg, Russia
Distribution: Ubuntu
Posts: 30

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:

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;
...but the script above by mmrtnt does the same in a much simpler way...

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


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 07:58 AM
slackware 13.0 32bit the "/usr/bin/sha1pass" perl script can NOT run ginkgo Slackware 3 03-05-2010 06:00 AM
Regarding # /opt/CTEact/bin/act & /opt/SUNWexplo/bin/explorer not available for x86 rajaniyer123 Solaris / OpenSolaris 6 04-24-2008 10:09 AM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 05:51 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 PM.

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