如何将系统命令的结果存储在Perl变量中? [英] How can I store the result of a system command in a Perl variable?
本文介绍了如何将系统命令的结果存储在Perl变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ cat test.pl
my $pid = 5892;
my $not = system("top -H -p $pid -n 1 | grep myprocess | wc -l");
print "not = $not\n";
$ perl test.pl
11
not = 0
$
我想捕获结果ie 11
到一个变量。我该如何做?
I want to capture the result i.e. 11
into a variable. How can I do that?
推荐答案
从 Perlfaq8 :
您混淆了 system()和反引号(``)。 system()运行命令并返回退出状态信息(作为16位值:低7位是进程死亡的信号,如果有的话,高8位是实际退出值)。反引号(``)运行一个命令,并返回它发送到STDOUT。
You're confusing the purpose of system() and backticks (``). system() runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value). Backticks (``) run a command and return what it sent to STDOUT.
$exit_status = system("mail-users");
$output_string = `ls`;
有很多方法可以从Perl执行外部命令。最常见的含义是:
There are many ways to execute external commands from Perl. The most commons with their meanings are: