Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1745
  • Last Modified:

Help! Writing script to show process id

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
jewee
Asked:
jewee
  • 2
1 Solution
 
rkosaiCommented:
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
 
ozoCommented:
$pid = `ps -ef -o pid,ppid,comm | grep xview | awk '{print \$1'}`;
0
 
ozoCommented:
#or
$pid = join"\n",map{(split)[0]}grep/xview/,`ps -ef -o pid,ppid,comm`;
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now