Re: piped system commands

2004-01-04 Thread deb
John, thanks for the perl approach.  Mustn't forget about that!


deb

At 20:59:59, on 01.02.04:
Cracks in my tinfoil beanie
allowed John W. Krahn to seep these bits into my brain:,
 Deb wrote:
  
  I want to run a command inside a script.  From the shell, here's the command:
  
  % ps -ef | /bin/egrep '/usr/lib/sendmail' | /bin/grep -v grep | /bin/awk '{print 
  $2}'
  19460
 
 open PS, 'ps -ef |' or die Cannot open pipe from 'ps -ef' $!;
 my $pid;
 while ( PS ) {
 next unless m|/usr/lib/sendmail|;
 $pid = ( split )[ 1 ];
 last;
 }
 close PS or die Cannot close pipe from 'ps -ef' $!;
 
 
 Or:
 
 my $pid;
 for ( `ps -ef` ) {
 next unless m|/usr/lib/sendmail|;
 $pid = ( split )[ 1 ];
 last;
 }
 
 
 Or:
 
 my ( $pid ) = map +( split )[ 1 ], grep m|/usr/lib/sendmail|, `ps -ef`;
 
 
 
 
 John
 -- 
 use Perl;
 program
 fulfillment
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 

-- 
  o  _ _ _
  _o /\_   _ \\o  (_)\__/o  (_)
_ \_   _(_) (_)/_\_| \   _|/' \/
   (_)(_) (_)(_)   (_)(_)'  _\o_
   http://zapatopi.net/afdb.html







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




Re: piped system commands

2004-01-02 Thread John W. Krahn
Deb wrote:
 
 I want to run a command inside a script.  From the shell, here's the command:
 
 % ps -ef | /bin/egrep '/usr/lib/sendmail' | /bin/grep -v grep | /bin/awk '{print $2}'
 19460

open PS, 'ps -ef |' or die Cannot open pipe from 'ps -ef' $!;
my $pid;
while ( PS ) {
next unless m|/usr/lib/sendmail|;
$pid = ( split )[ 1 ];
last;
}
close PS or die Cannot close pipe from 'ps -ef' $!;


Or:

my $pid;
for ( `ps -ef` ) {
next unless m|/usr/lib/sendmail|;
$pid = ( split )[ 1 ];
last;
}


Or:

my ( $pid ) = map +( split )[ 1 ], grep m|/usr/lib/sendmail|, `ps -ef`;




John
-- 
use Perl;
program
fulfillment

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




Re: piped system commands

2003-12-31 Thread deb
At 17:50:40, on 12.30.03:
Cracks in my tinfoil beanie allowed Andrew Gaffney to seep these bits into my brain:,
 
 Try changing the $2 to \$2. Perl is interpolating $2 before it gets to 
 bash, so bash sees /bin/awk '{print }'.
 
 -- 
Andrew,

Ah!!!  That was it.  I should have seen that.  Thanks so much
for pointing that out.

deb

-- 
  o  _ _ _
  _o /\_   _ \\o  (_)\__/o  (_)
_ \_   _(_) (_)/_\_| \   _|/' \/
   (_)(_) (_)(_)   (_)(_)'  _\o_
   http://zapatopi.net/afdb.html







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




Re: piped system commands

2003-12-31 Thread deb
At 15:58:26, on 12.30.03:
Cracks in my tinfoil beanie
allowed Bakken, Luke to seep these bits into my brain:,
 
 Instead of the useless 'grep -v grep', do this:
 
 % ps -ef | egrep '[/]usr/lib/sendmail' | awk '{print $2}'

Ah, yes, much cleaner.  Old habits die hard.  :-)

  But, when I put this into a test script, 
 
 Might I suggest finding the 'pid' file for sendmail and reading that
 instead? It should be under /var/run or some such directory...

Not as reliable as checking the process table.  The process could have been
killed off, but the pid file is still there.  

Thanks for the better regexp,

deb

-- 
  o  _ _ _
  _o /\_   _ \\o  (_)\__/o  (_)
_ \_   _(_) (_)/_\_| \   _|/' \/
   (_)(_) (_)(_)   (_)(_)'  _\o_
   http://zapatopi.net/afdb.html







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




Re: piped system commands

2003-12-31 Thread drieux
On Dec 31, 2003, at 9:04 AM, deb wrote:

Drieux,

Vladimir???
yes, named after vladimir ilyich,
it is my Sparc Box.
:-)

Thanks for the hints.  :-)


Personally I would be doing it with something like

	http://www.wetware.com/drieux/CS/Proj/Wetware_ps/

Which of course first started out as

	http://www.wetware.com/drieux/CS/Proj/PID/

Which of course started out with

	http://www.wetware.com/drieux/pbl/Sys/psGrovellor.txt

as the demonstration code for the idea, since if
one is going to be using Perl, why not do the
proc table munging in perl rather than outside of it
in a set of forked processes in a pipeline???
The alternative of course is to buy the learning
curve moment, pick up POE and 'be all that you can be'.
Or get the Proc::Table module from the CPAN.
ciao
drieux
we Blog, therefore we exist:

http://www.wetware.com/drieux/PR/blog2/

--

This space left intentionally blank.

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



Re: piped system commands

2003-12-30 Thread Andrew Gaffney
deb wrote:
Happy Almost New Year!

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

% 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, 

my $pid = `ps -ef | /bin/egrep '/usr/lib/sendmail' | /bin/grep -v grep | /bin/awk 
'{print $2}'`;
print \$pid is: $pid \n;
here's what I'm seeing ,

$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, and not doing the
awk '{print $2}'.   I've tried this with the system() call, with the same
results.  
Try changing the $2 to \$2. Perl is interpolating $2 before it gets to bash, so bash sees 
/bin/awk '{print }'.

--
Andrew Gaffney
System Administrator
Skyline Aeronautics, LLC.
776 North Bell Avenue
Chesterfield, MO 63005
636-357-1548


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



RE: piped system commands

2003-12-30 Thread Bakken, Luke
 I want to run a command inside a script.  From the shell, 
 here's the command:
 
 % ps -ef | /bin/egrep '/usr/lib/sendmail' | /bin/grep -v grep 
 | /bin/awk '{print $2}'
 19460

Instead of the useless 'grep -v grep', do this:

% ps -ef | egrep '[/]usr/lib/sendmail' | awk '{print $2}'

 But, when I put this into a test script, 

Might I suggest finding the 'pid' file for sendmail and reading that
instead? It should be under /var/run or some such directory...

Luke

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




Re: piped system commands

2003-12-30 Thread drieux
On Dec 30, 2003, at 3:54 PM, deb wrote:

Happy Almost New Year!
[..]
It seems to be only going as far as dropping off the grep, and not 
doing the
awk '{print $2}'.   I've tried this with the system() call, with the 
same
results.

What am I missing?   :-(
you have a shell interpret who to which problem.

vladimir: 65:] perl junk.plx
$pid is: 192
vladimir: 66:] sed 's/^/### /' junk.plx
### #!/usr/bin/perl -w
### use strict;
###
### my $cmd = q!ps -ef | egrep '[/]usr/lib/sendmail' | awk '{print $2}' 
!;
###
### my $pid = `$cmd`;
### print \$pid is: $pid \n;
###
###
vladimir: 67:]

Andrew's suggestion of guarding the $2 will also work.
I thought I would base my variant on Luke's somewhat tighter
general solution.
ciao
drieux
---

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