开发者

How can I get return value when using awk in perl script?

开发者 https://www.devze.com 2023-02-19 04:43 出处:网络
I am trying submit to $process the result of this system call my $process= system \"adb shell ps | egrep adb | awk开发者_开发技巧 \'{print $1}\' \";

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

0

精彩评论

暂无评论...
验证码 换一张
取 消