【问题标题】:perl input from STDIN pipe only works with perl in front of script来自 STDIN 管道的 perl 输入仅适用于脚本前面的 perl
【发布时间】:2014-03-05 14:55:26
【问题描述】:

我正在尝试使用 perl 脚本从 STDIN 读取数据。

这是我用于测试的脚本:

#!/usr/bin/perl

foreach $line (<>) 
{
  print $line;
}

所以在命令提示符下,我输入:

> cat somefile | testscript
>

它没有返回任何错误。

然后我输入以下内容:

> cat somefile | perl testscript
> *the contents of the file are printed line by line*

如果我不需要的话,我真的不希望用户必须在脚本前面附加 perl。

感谢阅读。

【问题讨论】:

  • 您可以通过执行while(&lt;&gt;) 并处理变量$_ 来解决此问题。 while(&lt;&gt;) 有很多神奇之处,如果您在另一个上下文中访问 &lt;&gt;,则不会发生这种情况。
  • @DovGrobgeld 你知道什么样的魔法吗?以上两个例子似乎都对我有用
  • 你确定在 /usr/bin/perl 有 perl 吗?有时它位于不同的位置(例如 /bin、/usr/local/bin)并且魔法不起作用。否则,请检查您的脚本是否为可执行文件(chmod 755 或类似)
  • 如果有帮助,我正在运行 Scientific Linux。
  • chmod 755 testscript; cat somefile | ./testscript

标签: perl command-line pipe


【解决方案1】:

我认为您运行两个不同版本的 Perl。 “which perl”的输出是什么?

【讨论】:

    猜你喜欢
    • 2012-06-12
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    相关资源
    最近更新 更多