Solved

Help!  Writing script to show process id

Posted on 2004-08-27
3
1,718 Views
Last Modified: 2012-08-13
Hello, I am writing a perl script that will just display the process id given a process name.

Here is a piece of it....

$pid = `ps -ef -o pid,ppid,comm | grep xview | awk '{print $1'}`;
print "pid = $pid\n";

now, when I just run this command via command line-->
ps -ef -o pid,ppid,comm | grep xview | awk '{print $1'}

it prints out only the process id number:
55386

However, when I run it in the script, it prints out:

55386 4593  xview
Am I using the wrong quotes or something?
0
Comment
Question by:jewee
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
rkosai earned 500 total points
ID: 11917809
The problem seems that Perl is using its own $1, which in Perl is the item captured by parenthesis in a regular expression.

You could instead use the system() command, with single quotes.  So instead of:

 $pid = `ps -ef -o pid,ppid,comm | grep xview | awk '{print $1'}`;

It would be:

my $pid = system('ps -ef -o pid,ppid,comm | grep xview | awk \'{print $1\'}'); #note the escaped quotes
0
 
LVL 84

Expert Comment

by:ozo
ID: 11918842
$pid = `ps -ef -o pid,ppid,comm | grep xview | awk '{print \$1'}`;
0
 
LVL 84

Expert Comment

by:ozo
ID: 11919019
#or
$pid = join"\n",map{(split)[0]}grep/xview/,`ps -ef -o pid,ppid,comm`;
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now