linux - Perl - 管道命令到另一个

标签 linux perl system pipe

快速提问,

有没有办法像在 *Nix 命令行中那样通过 perl 将一个命令通过管道传递给另一个命令?

例如:
免费-m | grep 内存

我怎样才能在 Perl 中进行“管道”操作?

最佳答案

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

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

来自documentation :

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:

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

关于linux - Perl - 管道命令到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4318208/

相关文章:

perl - 如何编写 CPAN 模块来支持多个 Perl 版本?

PHP exec() 在通过浏览器执行时不会执行 shell 命令

r - 如何使用 R 针对不同参数值绘制同一变量的多条曲线?

r - 在 Sys.sleep() 之前未执行的函数

linux - 查找命令不起作用

java - jsch 并从特定目录运行 sudo

c++ - 无法在 Linux 中运行使用 sfml 的程序

linux - 启动apache2.4时出错

perl - 如何在 Perl 中访问二进制标量的第 n 个字节?

Bash 在命令行上工作,但不在 perl 脚本中工作