Operating System - Linux
1752794 Members
5699 Online
108789 Solutions
New Discussion юеВ

pid in shell script again...

 
Karthik_sg
Frequent Advisor

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
3 REPLIES 3
Hemmetter
Esteemed Contributor

Re: pid in shell script again...

Hi

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

rgds
HGH
Padma Asrani
Honored Contributor

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
Victor Semaska_3
Esteemed Contributor

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.