Re: pid in shell script again... (13 Views)
Reply
Frequent Advisor
Karthik_sg
Posts: 68
Registered: ‎04-18-2007
Message 1 of 4 (13 Views)

pid in shell script again...

hi if thr r two process id s from the following command how to pick the second process id.
l=`ps ux | awk '/mojo/ && !/awk/ {print $2}'`
echo $l;
And if the o/p is
17665
17867
how to store this in an array and retrieve each one of thm whn needed
Please use plain text.
Esteemed Contributor
Hemmetter
Posts: 284
Registered: ‎10-17-2000
Message 2 of 4 (13 Views)

Re: pid in shell script again...

Hi

l=$(ps -C mojo -o pid= | sed -n "2p" )

rgds
HGH
Please use plain text.
Honored Contributor
Padma Asrani
Posts: 594
Registered: ‎01-18-2006
Message 3 of 4 (13 Views)

Re: pid in shell script again...

Hi

You can write a perl script as below.

#!/usr/local/bin/perl
@test = `ps ux | awk '/mojo/ && !/awk/ {print $2}'`;
foreach $test (@test)
{
print "$test\n";
}

save it to a file by name pid.pl
and chmod +x pid.pl
and invoke it
./pid.pl

HTH
Padma
Please use plain text.
Esteemed Contributor
Victor Semaska_3
Posts: 345
Registered: ‎09-09-2004
Message 4 of 4 (13 Views)

Re: pid in shell script again...

#!/bin/bash -vx

# Pick 2nd (last) PID.
l=`ps ux | awk '/root/ && !/awk/ {print $2}' | tail -1`
echo $l

# Place PIDs in array ary
l=`ps ux | awk '/root/ && !/awk/ {print $2}'`
ary=($l)

Vic
There are 10 kinds of people, one that understands binary and one that doesn't.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation