awk problem when attributing query to a variable

Hello,

I'm trying to make a script which reads the PID of a process using awk. I've read the thread in this forum where it is explained.
However, the problem i'm having is when attributing that PID value to a variable.
I'm using AIX 5.3.

The command is the following:
:/home/user1>ps -ef | grep xellerate | grep -i java | awk '{ print $2 }'
958598

Which as you can see.. works.

The problem i'm having is when I try to execute a script which is the following:

#!/bin/ksh
pid=""
pid='ps -ef && grep xellerate && grep -i java && awk '{print $2}''
echo $pid

:/home/user1> ./watchdog2
./watchdog2[3]: }: not found.

:/home/ias1013> pid='ps -ef && grep xellerate && grep -i java && awk '{print $2}''
/bin/ksh: }: not found.

AWK is not doing what I want it to.
Does anyone have any ideas why?

Thanks in advance,
Mambo

To have commands being executed/and substituted when defining the output to a variable, you have to enclose the whole in backticks like

var=`command`

or write it like this:

$(command)

You wrote the outer left and outer right of the commands with hard single quotes. This will not allow anything to be substituted in there.

Also please use [ code ] and [ /code ] tags when posting code, data, logs etc. to make it better visible, ty.

Thanks a lot, that solved my problem :slight_smile: