Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help!  Writing script to show process id

Posted on 2004-08-27
3
Medium Priority
?
1,742 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 2000 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 85

Expert Comment

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

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

971 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