How to run fswatch to call a program with static arguments?

Alternative answer not fighting xargs' default reason for being - passing on the output as arguments to the command to be run.

fswatch . | (while read; do python manage.py test; done)

Which is still a bit wordy/syntaxy, so I have created a super simple bash script fswatch-do that simplifies things for me:

#!/bin/bash  
(while read; do "$@"; done)

usage:

fswatch -r -o -e 'pyc' somepath | fswatch-do python manage.py test someapp.SomeAppTestCase


fswatch documentation (either the Texinfo manual, or the wiki, or README) have examples of how this is done:

$ fswatch [opts] -0 -o path ... | xargs -0 -n1 -I{} your full command goes here

Pitfalls:

  • xargs -0, fswatch -0: use it to make sure paths with newlines are interpreted correctly.
  • fswatch -o: use it to have fswatch "bubble" all the events in the set into a single one printing only the number of records in the set.
  • -I{}: specifying a placeholder is the trick you missed for xargs interpreting correctly your command arguments in those cases where you do not want the record (in this case, since -o was used, the number of records in the set) to be passed down to the command being executed.