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

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

Head First WordPress

Head First WordPress

Siarto Jeff / O'Reilly Media / 2010-02-15 / USD 34.99

Whether you're promoting your business or writing about your travel adventures, "Head First WordPress" will teach you not only how to make your blog look unique and attention-grabbing, but also how to......一起来看看 《Head First WordPress》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具