Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-12-2006, 03:52 PM   #1
LQ Newbie
Registered: Dec 2006
Location: Tehran, Iran
Distribution: Ubuntu
Posts: 5

Rep: Reputation: 0
Unhappy 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:

$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

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

and so the $pid variable remains empty.

Any ideas?
Old 12-12-2006, 03:56 PM   #2
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
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:

pid=$(ps x | grep ./program | grep $param_name | gawk '{ print $1 }')
pid=$(ps x | grep ./program | grep $param_name | gawk "{ print \$1 }")
Old 12-12-2006, 04:57 PM   #3
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Just an idea: why not use pgrep?

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

pgrep, pkill -- find or signal processes by name
You'll probably find it on your system even if it's not FreeBSD.
Old 12-12-2006, 06:42 PM   #4
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I second pgrep
Old 12-13-2006, 01:13 AM   #5
LQ Newbie
Registered: Dec 2006
Location: Tehran, Iran
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks to macemoneta
but unfortunately both ways did nothing

I'm trying pgrep and pkill

Thanks to taylor_venable

Last edited by _hadi_; 12-13-2006 at 01:17 AM.
Old 12-13-2006, 01:25 AM   #6
LQ Newbie
Registered: Dec 2006
Location: Tehran, Iran
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks Pkill was great.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
awk on a variable onradius Programming 8 02-22-2006 09:34 AM
[Bash] Concatenate string using awk senorsnor Programming 7 05-05-2005 01:38 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 02:54 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM
Bash Variable Problem decates Linux - Newbie 10 01-28-2003 11:47 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration