2

I want to use fswatch to run a file through my unit test framework whenever I save.

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 . | 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:

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

What am I doing wrong?

Edit:

fswatch + grep works fine by itself

$ fswatch . | grep test
/my/path/test.txt
Jesse Aldridge
  • 7,991
  • 9
  • 48
  • 75
  • `-0` is for `NULL` de-limiting, the [official-page](https://github.com/emcrisostomo/fswatch) says, use `fswatch -o path` ? Is the result the same? – Inian Jan 25 '17 at 09:05
  • `fswatch -o . | xargs -I {} echo {} {}` then I get `1 1` as output every time I save – Jesse Aldridge Jan 25 '17 at 09:09

2 Answers2

2

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.

Ipor Sircer
  • 3,069
  • 3
  • 10
  • 15
  • Good idea; it seems almost better than the native `fswatch` options (`--include`, `--exclude`), although I really can't say for sure... +1 – l'L'l Jan 25 '17 at 10:34
  • 1
    As I explained, it's not "better", unless you're sure about the characters contained in all the file names that will be output by `fswatch`. Assuming you don't have this kind of control, you should not line buffer light-heartedly, otherwise you may very well hit all the issues that are taken care of by filters and `fswatch` option `-0`. – Enrico M. Crisostomo Jan 25 '17 at 10:47
1

As already said, --line-buffered tells grep not to block-buffer output, hence writing entire lines immediately to standard output. 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.

Now: as explained in fswatch documentation, file names can contain new line characters. Hence the existence of -0 (as other file-handling utilities such as find have). If you tell grep to line-buffer and you hit such a file you will end up with a corrupt fswatch record.

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.

Enrico M. Crisostomo
  • 1,653
  • 1
  • 17
  • 26