Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help!  Writing script to show process id

Posted on 2004-08-27
3
Medium Priority
?
1,741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 …
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.…
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

715 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