# Variable to command to Variable Question KSH

**URL:** <https://community.unix.com/t/variable-to-command-to-variable-question-ksh/308396>\
**Category:** Shell Programming and Scripting\
**Created:** [April 15, 2012, 11:02am UTC](https://community.unix.com/t/variable-to-command-to-variable-question-ksh/308396 "2012-04-15T11:02:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Coyote270WSM](https://community.unix.com/letter_avatar/coyote270wsm/32/6_449c533f586939b6e5f246632a46421f.png) [@Coyote270WSM](https://community.unix.com/u/Coyote270WSM)\
**Post date:** [April 15, 2012, 11:02am UTC](https://community.unix.com/t/variable-to-command-to-variable-question-ksh/308396/1 "2012-04-15T11:02:31Z")

</div>

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.

```nohighlight
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.

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 15, 2012, 11:11am UTC](https://community.unix.com/t/variable-to-command-to-variable-question-ksh/308396/2 "2012-04-15T11:11:42Z")

</div>

To pass a variable, try this..:

```nohighlight
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 🙂 )

---

<div class="post-metadata">

**Author:** ![47shailesh](https://community.unix.com/letter_avatar/47shailesh/32/6_449c533f586939b6e5f246632a46421f.png) [@47shailesh](https://community.unix.com/u/47shailesh)\
**Post date:** [April 15, 2012, 11:12am UTC](https://community.unix.com/t/variable-to-command-to-variable-question-ksh/308396/3 "2012-04-15T11:12:44Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Coyote270WSM](https://community.unix.com/letter_avatar/coyote270wsm/32/6_449c533f586939b6e5f246632a46421f.png) [@Coyote270WSM](https://community.unix.com/u/Coyote270WSM)\
**Post date:** [April 15, 2012, 11:24am UTC](https://community.unix.com/t/variable-to-command-to-variable-question-ksh/308396/4 "2012-04-15T11:24:38Z")

</div>

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.
