1847935 Members
4248 Online
104021 Solutions
New Discussion

quick question

 
SOLVED
Go to solution
steven Burgess_2
Honored Contributor

quick question

Hi all

I'm trying to create a variable 'pid' from the output of the command

echo $port
1p15

when i do

ps -ef | grep $port, i get the output from the grep command plus my grep

ie

root 880 1 0 Mar 24 ? 0:25 ocd -n***.**.***.*** -f/dev/telne
t/1p15 -b01 -p15 -c/etc/ddfa
root 17786 10571 1 05:29:53 pts/0 0:00
grep 1p15

I want to create a variable $pid using

pid=`ps -ef | grep $port | awk '{ print $2 }'`

echo $pid
880 17253

why do I get the first 2 columns from both lines and not just the first?

How can I get $pid to equal the 2nd column of the 1st line?

TIA

Steve
take your time and think things through
2 REPLIES 2
S.K. Chan
Honored Contributor
Solution

Re: quick question

Instead of ..

pid=`ps -ef | grep $port | awk '{ print $2 }'`

you would do ..

pid=`ps -ef | grep $port | grep -v grep | awk '{ print $2 }'`

The "grep -v grep" will filter out the 2nd line.

steven Burgess_2
Honored Contributor

Re: quick question

logical

thanks

Steve
take your time and think things through