12

我正在尝试使用fswatch从 linux bash 脚本中翻译以下行,以便能够在 Mac OSX 上运行它:

inotifywait -r -m 'myfolder/' | while read MODFILE
do
    echo "something"
done

由于 inotifywait 在 Mac OSX 上不起作用,我想用第一行替换FSWatch。尽管 README 引用了 fswatch 手册页,但它没有提供指向它的链接,并且在 Internet 上进行搜索也没有得到任何信息。所以我试着弄乱了一点。

我创建了一个名为的文件夹testfswatch/,并尝试在父文件夹中运行以下命令,然后在该文件夹中添加和更改文件testfswatch/

fswatch -o testfswatch/ | echo "LALA"
fswatch -o testfswatch/ | someSimpleProgram
fswatch -o testfswatch/ | xargs -n1 -I{} echo "LALA"
fswatch -o testfswatch/ | xargs -n1 -I{} ./someExecutable
fswatch -o testfswatch/ | xargs -n1 echo "LALA"
fswatch -o testfswatch/ | xargs -n1 someExecutable
// And more variations of the above

但是,当我在文件夹中更改或添加文件时,这些命令似乎都没有做任何事情tesfswatch/

有人知道我在这里做错了什么吗?欢迎所有提示!

4

2 回答 2

20

As already said by Bushmills, fswatch syntax has changed in v. >= 1.x after it was merged with fsw: the rationale behind this decision was allowing more than one path to be watched using a syntax that felt natural to UNIX users.

Furthermore, one specific option (-0) was added to allow easy and non-error-prone piping of fswatch output to another program's input: when using -0, fswatch will separate records using the NUL (\0) character (along the lines of what other utilities such as find and xargs do).

In your case, then, you probably want to use the following syntax (fine tune it according to your needs):

$ fswatch -0 [opts] [paths] | xargs -0 -n 1 -I {} [command]

This way:

  • fswatch -0 will split records using the NUL character.
  • xargs -0 will split records using the NUL character. This is required to correctly match impedance with fswatch.
  • xargs -n 1 will invoke command every command. If you want to do it every x records, then use xargs -n x.
  • xargs -I {} will substitute occurrences of {} in command with the parsed argument. If the command you're running does not need the event path name, just delete this option.

Hope this helps.

于 2014-08-27T09:53:44.863 回答
2

与 1.0 及更高版本相比,fswatch 1.0 之前的版本使用稍微不同的调用语法。确保您运行的版本高于 1.0,如果您的版本早于该版本,您可能需要升级。

于 2014-07-04T12:45:51.350 回答