1

I have a bash script where I want to trigger a method every time the filesystem is changed:

#!/bin/bash

function run {
    echo Do some magic
    // Do some magic
}

run

fswatch . src | 'run'

In this case I am watching the src folder.

When I trigger the script via

./automater.sh

the script executes the run function the first time correctly and when I then change some file, the script simply exits...

Leon
  • 31,443
  • 4
  • 72
  • 97
Creative crypter
  • 1,348
  • 6
  • 30
  • 67
  • i figured out that when i use *fswatch . src | ./automater.sh run*, it works fine, BUT then it runs in a loop.... why? – Creative crypter Sep 01 '17 at 11:38
  • Does your `run` function contain a loop reading the events from standard input? If not, then your code must be changed to `fswatch . src | while read -r changed_path; do run; done` – Leon Sep 01 '17 at 11:39

1 Answers1

2

BUT then it runs in a loop...., I also hit this, because after your ./automater.sh execution, there are some new file changes in your directory, you can use exclude option to ignore these files, in my case like:

fswatch -l 5 -o -e ".*" -i "\\.py$" . | while read; \ do \ unittest || true done

  • -l, check file changes every 5 seconds
  • -o, to batch all changes to one number
  • -e, exclude file, ".*" means exclude all files firstly
  • -i, include file, "\.py$" means include all .py files
  • || true, seems need to make your command return true when your use in shell script, I use this trick to solve only first time correctly issue

Also you can run fswatch . src and your 'run' command in separate terminal window to find what are the newly changes are.

hanks
  • 71
  • 6