【问题标题】:Piping output of shell command to grep causes "grep: write error"shell命令的管道输出到grep导致“grep:写入错误”
【发布时间】:2017-03-24 19:49:43
【问题描述】:

我在 Crystal 中编写了一个工具,它接受一些命令行参数,基本上将这些参数转换为“find stuff | xargs grep”,其中 xargs 被指示使用多个进程。这是通过 Process.run 运行的,输出和错误被重定向到一个自定义的 IO 对象中,该对象过滤了 grep 的内容,将所有未过滤的内容写入 STDOUT。

当我正常运行它时,它似乎运行良好。不过,在搜索完成之前似乎确实有一些输出被切断的情况,所以我不确定我是否可以完全相信结果。但是,当我将此命令的输出通过管道传输到 grep 时,它总是会提前中断搜索并显示“grep:写入错误”。我不知道为什么会这样,并且希望得到一些帮助。最终我可能会重写它以在纯 Crystal 中执行所有操作,但现在这是搜索我正在处理的代码库的快速解决方案。

这是正在运行的代码:

class FindFilterIO
include IO

@@generic_filter = [".diff", ".iml", "/target/"]
@@web_filter = [".css", ".js", ".jsp", ".ftl"]

def initialize(@web_search : Bool = false)
end

def read(slice : Bytes)
  raise "FindFilterIO does not support reading!"
end

def write(slice : Bytes)
  str = String.new slice
  if @@generic_filter.any? { |e| str.includes? e }
    return
  end

  if @web_search
    if !@@web_filter.any? { |e| str.includes? e }
      return
    end
  end

  STDOUT.write(slice)
end
end

  cmd = "find . -not \\( -path ./.svn  -prune \\) " \
        "-not \\( -path ./.idea -prune \\) " \
        "-type f -print0 " \
        "| xargs -0 -P 1 -n 100 grep -E -n --color=always "
  cmd += if @html_id
           "'id=['\"'\"'\"]#{@search_text}['\"'\"'\"]|\##{@search_text}'"
         elsif @html_class
           "'class=['\"'\"'\"]#{@search_text}['\"'\"'\"]|\\.#{@search_text}'"
         else
           "'#{@search_text}'"
         end
  io = FindFilterIO.new web_search: (@html_id || @html_class)
  Process.run(cmd, output: io, error: io, shell: true, chdir: File.join(@env.home_dir, @env.branch, "repodir"))

【问题讨论】:

标签: crystal-lang


【解决方案1】:

现在https://github.com/crystal-lang/crystal/issues/2065 的问题已关闭,这似乎已得到解决。需要进行更多测试以确保它完全修复,但现在使用我的旧代码似乎可以正常工作。

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多