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

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

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.

set a = `ps aux | awk '/test5/ && \!/awk/ {print $2}'`
# 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 .