Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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.