LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Awk Variable problem (https://www.linuxquestions.org/questions/programming-9/bash-awk-variable-problem-509797/)

_hadi_ 12-12-2006 02:52 PM

Bash Awk Variable problem
 
Dear all,
I want to find a PID of a certain program.
I have same program running with different parameters and want to find the PID based on the parameter

./program conf1.conf
./program conf2.conf
./program conf3.conf

So I Used:

#!/usr/bash
...
$param_name = $1
pid=$(ps x | grep ./program | grep $param_name | gawk "{ print $1 }")
...

My problem is that when I use this command in my script file,
$1 is replaced by the script's frist parameter

I mean

./myscript conf1
causes

pid=$(ps x | grep ./program | grep conf1 | gawk "{ print conf1 }")

and so the $pid variable remains empty.

Any ideas?

macemoneta 12-12-2006 02:56 PM

You are not escaping the $ or using single quotes to prevent substitution, so of course the parameter is substituted. You want to pass the string '$1' to gawk, not the value of $1.

Either of these should work:

Code:

pid=$(ps x | grep ./program | grep $param_name | gawk '{ print $1 }')
pid=$(ps x | grep ./program | grep $param_name | gawk "{ print \$1 }")


taylor_venable 12-12-2006 03:57 PM

Just an idea: why not use pgrep?

Quote:

PKILL(1) FreeBSD General Commands Manual PKILL(1)

NAME
pgrep, pkill -- find or signal processes by name
You'll probably find it on your system even if it's not FreeBSD.

matthewg42 12-12-2006 05:42 PM

I second pgrep

_hadi_ 12-13-2006 12:13 AM

Thanks to macemoneta
but unfortunately both ways did nothing :(

I'm trying pgrep and pkill

Thanks to taylor_venable :)

_hadi_ 12-13-2006 12:25 AM

Thanks Pkill was great.


All times are GMT -5. The time now is 09:52 AM.