0

I am trying submit to $process the result of this system call

my $process= system "adb shell ps | egrep adb | awk '{print $1}' ";

but when print " $process \n"; I have got zero

Any suggestions

Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
Koby
  • 17
  • 1
  • 7

3 Answers3

1

I don't think perl captures output when you use system() calls.

Try wrapping it in backticks instead:

my $process = `adb shell ps | egrep adb | awk '{print $1}'`;
Rob Williams
  • 1,442
  • 12
  • 13
1

The return value of system() is the exit status of the program (here). Use backtick operation instead:

$process = `...`;
pmod
  • 10,450
  • 1
  • 37
  • 50
  • Ok It's look like that $process getting the egrep adb result without consider the awk – Koby Mar 24 '11 at 07:45
  • So when calling my $process= `adb shell ps | egrep adb | awk '{print \$1}` I have got what I need, Thanks – Koby Mar 24 '11 at 07:48
0

I just found a much more detailed explanation on SO itself. Editing to add that link - What's the difference between Perl's backticks, system, and exec?


What pmod has mentioned is correct. Since I have been doing a bit of reading on this lately, just adding a comment with what I found:

system

executes a command and your perl script is continued after the command has finished. It returns the exit status of the command.

backticks - ` `

This is kind of like system, executes the command which you launch and waits for it to return. However, unlike system, returns STDOUT for the command. Which I presume is what you are looking for here.

exec

replaces current with new process and doesn't return anything.

Hope that helps ...

Community
  • 1
  • 1
Bee
  • 958
  • 5
  • 12