在 osx 中使用 xargs 接受 fswatch 中已更改文件的名称

Ube*_*bug 5 macos bash fswatch

在 osx 中,我试图检测何时将新文件添加到文件夹中,然后执行脚本来处理该新文件。看起来很简单,对吧?

我正在这样做:

fswatch  -0  ~/motion-detection | xargs -0 -n1 -I {} ./detectmotion.sh
Run Code Online (Sandbox Code Playgroud)

它将调用 shell 脚本 detectormotion.sh,其中包含:

echo "File added: " $1
Run Code Online (Sandbox Code Playgroud)

至少现在是这样。我只想将更改后的文件的文件名传递给 detectormotion.sh

我花 1 美元得到一个空白

谁能看到我在这里做错了什么吗?

提前致谢!

Ini*_*ian 5

它应该为,因为您没有将参数传递给detectmotion.sh您从 获得的脚本xargs。本来应该是

fswatch  -0  ~/motion-detection | xargs -0 -n1 -I {} ./detectmotion.sh "{}"
Run Code Online (Sandbox Code Playgroud)

请记住,-I {}in 中的标志xargs代表通过管道传输到 的值的占位符xargs。因此,在您尝试作为问题的一部分时,您只是将值存储在 place-holder( {}) 中,而不是实际将其传递给脚本以使其有用。

因此,我修改了该命令,以便您实际上将接收到的值传递给脚本,现在可以在$1脚本内部访问时打印该值。

引用自man xargs页面-I

-I replace-str
     Replace  occurrences  of  replace-str  in the initial-arguments with names read from 
     standard input.  Also, unquoted blanks do not terminate input items; instead the 
     separator is the newline character.  Implies -x and -L 1.
Run Code Online (Sandbox Code Playgroud)

在我们的例子中,replace-str被用作{}.