scripted piped system commands

From: Deb (beginners_at_perl.org)
Date: 12/31/03


Date: Wed, 31 Dec 2003 07:55:29 -0800
To: Perl List <beginners@perl.org>

Happy Almost New Year!

I want to run a unix system command inside a script. From the
shell, here's the command(s):

% ps -ef | /bin/egrep '/usr/lib/sendmail' | /bin/grep -v grep | /bin/awk '{print $2}'
19460

What is returned is the pid of the process being grep'd.

But, when I put this into a test script,

---- snip ----

my $pid = `ps -ef | /bin/egrep '/usr/lib/sendmail' | /bin/grep -v grep | /bin/awk '{print $2}'`;
print "\$pid is: $pid \n";

---- snip ----

And, here's the output:

$pid is: root 19460 1 0 Dec 18 ? 0:08 /usr/lib/sendmail -bd
-q15m

It seems to be only going as far as dropping off the grep (grep -v),
and never executing the awk '{print $2}'. I've tried this with
the system() call, with the same results.

Please, what am I missing? :-(

deb



Relevant Pages

  • Re: service; ps & grep help
    ... the actual grep itself, ... custom start and stop script for. ... kill -9 $pid ... I assume the correct way to do this is store the pid in a file that you ...
    (Fedora)
  • Re: how to run scripts uniquely
    ... was running (using grep and wc) but I now see that sometimes the script ... we get the below where it appears twice in the list of processes... ... #write out pid ...
    (Debian-User)
  • Re: When does the Dock save preferences?
    ... >> With Keyboard Maestro, it's one keystroke, so fairly. ... > And you'll be knows how to GREP the PID as it changes each time it ... Wouldn't dream of it- but is there no way of using a script to get that ... Not something I'd think of trying myself, but if the Dock has a PID, ...
    (uk.comp.sys.mac)
  • simple script that kills a process
    ... I am trying to write a very simple script that kills a process: ... The output of grep looks like ... The PID is 1611 but I don't succed to grab it using "cut". ...
    (comp.unix.shell)
  • Re: LTT user input
    ... and to that end have taken the existing trace infrastructure ... script for that event (if there's a handler defined for the event ... This gives the script a chance to do whatever Perlish thing it ... syscall totals to individual syscall totals for each pid. ...
    (Linux-Kernel)