【问题标题】:How to print file path that caused fswatch to do something如何打印导致 fswatch 执行某些操作的文件路径
【发布时间】:2016-02-14 18:49:36
【问题描述】:

我使用fswatch 在文件更改时采取一些措施。我使用以下命令:

fswatch -o -r '.' | while read MODFILE
do
    echo 'Some file changed, so take some action'
done

这很好用,如果我更改几个文件,我会在终端中看到以下内容:

Some file changed, so take some action
Some file changed, so take some action
Some file changed, so take some action
etc.

但我也想知道实际上是哪个文件导致了该操作。所以我检查了 fswatch 手册页,显示如下:

fswatch writes a record for each event it receives containing:
     -       The timestamp when the event was received (optionally).
     -       The path affected by the current event.
     -       A space-separated list of event types (see EVENT TYPES ).

但正如我所说,我的终端中没有列出任何内容。有人知道我如何显示“受当前事件影响的路径。”吗?

欢迎所有提示!

【问题讨论】:

  • @MarkSetchell - 感谢您的建议,但不幸的是仍然没有结果..
  • 使用 fswatch 1.8.0 在 El Capitan 上运行良好 - 只需在没有 while read ... 的情况下运行它
  • @MarkSetchell - 啊,没有while read.. 它确实有效!但我确实需要while read 才能真正采取行动,对吗?您知道如何在打印文件路径的同时编辑命令以执行操作吗?

标签: linux macos inotify inotifywait fswatch


【解决方案1】:

当然,使用这个:

fswatch --batch-marker=EOF -xn . | while read file event; do 
   echo $file $event
   if [ $file = "EOF" ]; then 
      echo TRIGGER
   fi
done

如果您想将受影响文件的名称保存在列表中,您可以这样做:

#!/bin/bash
fswatch --batch-marker=EOF -xn . | while read file event; do
    if [ $file = "EOF" ]; then
       echo TRIGGER
       echo Files: "${list[@]}"
       list=()
    else
       echo $file $event
       list+=($file)
    fi
done

【讨论】:

  • 但是使用此代码,它会对所有已更改的文件(如果它超过一个)执行操作(这对我来说通常是这种情况)。我真正需要的是代码首先列出所有已更改的文件,然后只执行一次操作。这也可能吗?
  • 您可以使用fswatch --batch-marker=EOF 添加批处理标记,然后您将在批处理结束时获得所有文件和EOF,您可以使用它来触发您的处理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2011-09-26
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多