Output of ps + awk in a variable to get PID | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Output of ps + awk in a variable to get PID

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-10-2009
amio amio is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 20 September 2010, 12:59 AM EDT
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Output of ps + awk in a variable to get PID

Hi All,

I am getting the PID of a process using its name using the following command
in a script


Code:
mypid=`ps aux | awk '/test5/ && !/awk/ { print $2 }'`
echo PID is $mypid

The problem is the PID is not getting printed. But when i run the command directly in shell, the PID got printed.

May i know where am going wrong in the script code?

Thanks in advance.
Amio
Sponsored Links
    #2  
Old 03-10-2009
Digby Digby is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 20 July 2009, 8:49 AM EDT
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
What kind of script is it? It's probably not that same as the shell you are working from.

Your command wouldn't work in my console. But it did when I changed it to this.


Code:
set a = `ps aux | awk '/test5/ && \!/awk/ {print $2}'`


Last edited by Digby; 03-10-2009 at 11:54 AM..
Sponsored Links
    #3  
Old 03-10-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 29 July 2014, 6:33 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Quote:
Originally Posted by amio View Post
The problem is the PID is not getting printed. But when i run the command directly in shell, the PID got printed.

Code:
# mypid=`ps aux | awk '/sshd/ && !/awk/ { print $2 }'`
# echo PID is $mypid
PID is 2077 2801 2806 2985 3410

Read man ps for -w option.
In the same time you should read man pgrep .
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK Output into a variable mace_560 Shell Programming and Scripting 2 12-30-2008 03:58 AM
Put the output of grep in a variable big123456 Shell Programming and Scripting 3 04-23-2008 11:40 AM
Set a variable from awk output Cranie UNIX for Dummies Questions & Answers 3 10-11-2007 09:39 AM
storing output of awk in variable mab_arif16 Shell Programming and Scripting 3 05-07-2006 06:15 PM
how to output awk to a variable bashirpopal UNIX for Dummies Questions & Answers 4 04-02-2003 10:02 AM



All times are GMT -4. The time now is 01:22 AM.