如何将系统命令的结果存储在Perl变量中?

栏目: Perl · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/3854651/how-can-i-store-the-result-of-a-system-command-in-a-perl-variable
$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
$

我想把结果,即11变成一个变量.我怎样才能做到这一点?

Perlfaq8

你很混淆 system()backticks (“)的目的. system()运行命令并返回退出状态信息(作为16位值:低7位是进程死亡的信号,如果有的话,高8位是实际退出值).反引号(“)运行命令并将其发送到STDOUT.

$exit_status   = system("mail-users");
    $output_string = `ls`;

有很多方法可以从 Perl 执行外部命令.最有意义的是:

> system() :你想执行一个命令,不想捕获它的输出

> exec :你不想返回

调用perl脚本

> backticks :你想捕捉

输出命令

> open :你想要管道命令(as

输入或输出)到您的脚本

另见 How can I capture STDERR from an external command?

http://stackoverflow.com/questions/3854651/how-can-i-store-the-result-of-a-system-command-in-a-perl-variable


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to Genetic Algorithms

An Introduction to Genetic Algorithms

Melanie Mitchell / MIT Press / 1998-2-6 / USD 45.00

Genetic algorithms have been used in science and engineering as adaptive algorithms for solving practical problems and as computational models of natural evolutionary systems. This brief, accessible i......一起来看看 《An Introduction to Genetic Algorithms》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具