Perl-将命令传递到另一个 [英] Perl - Pipe command into another

查看:80
本文介绍了Perl-将命令传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问

是否可以像在* Nix命令行中那样,通过perl将命令通过管道传递到另一个命令中?

Is there a way to pipe a command into another command via perl like you can in the *Nix command line?

例如:
free -m | grep Mem

我将如何在Perl中进行配管"?

How would I be able to do the "piping" in Perl?

推荐答案

您可以像这样完全调用命令:

You can invoke the command exactly like that:

system("free -m | grep Mem");

文档:

如果只有一个标量参数,则检查该参数是否包含shell元字符,如果有,则将整个参数传递至系统的命令外壳程序进行解析(在Unix平台上为/bin/sh -c,但在其他平台上会有所不同).如果参数中没有外壳元字符,则将其拆分为单词,然后直接传递给execvp,这样效率更高.

If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to execvp , which is more efficient.

您可以使用其他方法来调用外部命令,例如open:

You can do the same with other methods for invoking external commands, like open:

open my $fh, "-|", "free -m | grep Mem" or croak "failed to run pipeline";
# and now read from $fh as usual

这篇关于Perl-将命令传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆