快速提问,
有没有办法像在 *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/