简体   繁体   English

带grep的fswatch未管道

[英]fswatch with grep not piping

I want to use fswatch to run a file through my unit test framework whenever I save. 每当保存时,我都想使用fswatch通过我的单元测试框架运行文件。

As a test, I run fswatch and make a minor change to /my/path/test.txt in my text editor and get the output I expected: 作为测试,我运行fswatch并在文本编辑器中对/my/path/test.txt进行了较小的更改,并获得了预期的输出:

$ fswatch . | xargs -I {} echo {} {}
/my/path/test.txt /my/path/test.txt

But if I insert a grep in the middle and repeat the process I get no output: 但是,如果我在中间插入一个grep并重复该过程,则不会得到任何输出:

$ fswatch . | grep test | xargs -I {} echo {} {}

What am I doing wrong? 我究竟做错了什么?

Edit : 编辑

fswatch + grep works fine by itself fswatch + grep本身可以正常工作

$ fswatch . | grep test
/my/path/test.txt

man grep: man grep:

   --line-buffered
          Use line buffering on output.   This  can  cause  a  performance
          penalty.

When using grep as last pipe command then linebuffered is the deault. 当使用grep作为最后一个管道命令时,线缓冲就是默认值。

As already said, --line-buffered tells grep not to block-buffer output, hence writing entire lines immediately to standard output. 如前所述,-- --line-buffered告诉grep 执行块缓冲区输出,因此将整行立即写入标准输出。 Block-buffering is the default behaviour when grep is writing to a pipe, line-buffering is the default when it's writing to a terminal. grep写入管道时,块缓冲是默认行为,当写入终端时,行缓冲是默认行为。

Now: as explained in fswatch documentation, file names can contain new line characters. 现在:如fswatch文档中所述,文件名可以包含换行符。 Hence the existence of -0 (as other file-handling utilities such as find have). 因此,存在-0 (如find等其他文件处理实用程序一样)。 If you tell grep to line-buffer and you hit such a file you will end up with a corrupt fswatch record. 如果您将grep告诉行缓冲区, 并且您命中了这样的文件,则最终将导致fswatch记录损坏

Why aren't you filtering using filters instead? 为什么不使用过滤器进行过滤? For simple filtering they're easy to use. 对于简单的过滤,它们易于使用。 That way fswatch will internally match file names without your having to worry about these peculiarities and corner-cases. 这样, fswatch将在内部匹配文件名,而您不必担心这些特性和特殊情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM