gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:29:34 24 4
gpt4 key购买 nike

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

我这样做:

fswatch  -0  ~/motion-detection | xargs -0 -n1 -I {} ./detectmotion.sh

这将调用 shell 脚本 detectmotion.sh,它包含:

echo "File added: " $1

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

1 美元我得到一张空白

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

提前致谢!

最佳答案

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

fswatch  -0  ~/motion-detection | xargs -0 -n1 -I {} ./detectmotion.sh "{}"

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

所以我修改了命令,以便您实际上将接收到的值传递给脚本,现在可以在脚本中以 $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.

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

关于bash - 在 osx 中使用 xargs 接受 fswatch 中更改文件的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42410454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com