Variable to command to Variable Question KSH

Hello,

First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID, there are no errors.

ps -ef | awk '{if ($2 == "3026") print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}'

If I pass it as a variable that I know contains data, it errors out. Best as I can tell, the data isn't being read.

ps -ef | awk '{if ($2 == $mFName) print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}'

The only thing that I can think that is happening is that the command is running outside of the main script and I need to pass the value to it in some way. I just haven't been able to figure out how to do it.

I then want to pass the results to a variable that I can use.

Can anyone point the way or provide an alternate explanation? Any assistance would be greatly appreciated.

To pass a variable, try this..:

ps -ef | awk '{if ($2 == mfname) print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}' mfname="$mFName"

The double quotes are important..

(BTW welcome to these forums and I salute you for correctly using code tags right in your first post :slight_smile: )

you need to define variable in awk so that it can understand:

ps -ef | awk  -v name="$nFName" '{if ($2 == name) print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}'

Thank you for the help. I tested and it worked very well. This is definately going on my offices wiki for one to remember.

---------- Post updated at 09:24 AM ---------- Previous update was at 09:21 AM ----------

I may have botched the reply, however, I wanted you both to know that your assistance was very timely and much needed. Both solutions worked as advertised. Again, much thanks. Now I can start hunting why some of my Oracle processes are crashing. I needed that line so that I could give a face to the PID I was using LSoF with.