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

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

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


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

查看所有标签

猜你喜欢:

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

Database Design and Implementation

Database Design and Implementation

Edward Sciore / Wiley / 2008-10-24 / 1261.00 元

* Covering the traditional database system concepts from a systems perspective, this book addresses the functionality that database systems provide as well as what algorithms and design decisions will......一起来看看 《Database Design and Implementation》 这本书的介绍吧!

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

在线图片转Base64编码工具

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

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具