How can I get return value when using awk in perl script?
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
The return value of system() is the exit status of the program (here). Use backtick operation instead:
$process = `...`;
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}'`;
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 ...
精彩评论