shell - 在 Perl 中打开管道是否涉及 shell?

标签 shell perl unix pipe

如果我在 Unix/Linux 系统上的 Perl 脚本中执行此操作:

open(my $fh, 'cat|');

是否涉及 shell ?如果没有,那怎么办:
open(my $fh, 'cat -v|');

如果可能的话,我想避免使用 shell,我什至知道该怎么做:
open(my $fh, '-|') || exec('cat', '-v');

但简洁也很有值(value)。

最佳答案

来自 open , 下面的代码示例

The last two examples in each block show the pipe as "list form", which is not yet supported on all platforms. A good rule of thumb is that if your platform has a real fork (in other words, if your platform is Unix, including Linux and MacOS X), you can use the list form. You would want to use the list form of the pipe so you can pass literal arguments to the command without risk of the shell interpreting any shell metacharacters in them. However, this also bars you from opening pipes to commands that intentionally contain shell metacharacters, [...]



(我的重点)

在此之前的最后一个例子是

open(my $fh, "-|", "cat", "-n", $file);


顺便说一下,这几乎正是你从问题中得到的例子。

所以使用“列表形式”并且没有 shell 。这适用于 system以及(但是,唉,不适用于 qx )

关于shell - 在 Perl 中打开管道是否涉及 shell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59549063/

相关文章:

bash - 通过 SSH 连接到 guest 计算机后继续执行 shell 脚本吗?

linux - sed如何在匹配模板时插入2次替换?

c - mktime() 返回大量秒数

linux - 如何列出 Linux 组中的所有用户?

linux - 数据库备份未写入光盘,空间不足?

Bash测试值评估

shell - @D 在 shell 脚本中是什么意思

perl - 我应该使用哪个 Perl Web 框架?

xml - 向 XML::LibXML 对象添加新节点/元素

perl - 有什么方法可以在 Moose 对象中使用 Moose::Exporter 吗?