LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-12-2006, 02:52 PM   #1
_hadi_
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:

#!/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?
 
Old 12-12-2006, 02:56 PM   #2
macemoneta
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: 334Reputation: 334Reputation: 334Reputation: 334
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 }")
 
Old 12-12-2006, 03:57 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.
 
Old 12-12-2006, 05:42 PM   #4
matthewg42
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, 12:13 AM   #5
_hadi_
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 12:17 AM.
 
Old 12-13-2006, 12:25 AM   #6
_hadi_
LQ Newbie
 
Registered: Dec 2006
Location: Tehran, Iran
Distribution: Ubuntu
Posts: 5

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


Reply


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 08:34 AM
[Bash] Concatenate string using awk senorsnor Programming 7 05-05-2005 12:38 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
Bash Variable Problem decates Linux - Newbie 10 01-28-2003 10:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 AM.

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