Solved

Help!  Writing script to show process id

Posted on 2004-08-27
3
1,725 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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