6

So I'm doing a simple inotifywait loop to watch for changes in a Bootstrap directory:

while inotifywait -r -q --format %w bootstrap/; do
    echo "something happened"
    [[ $filename == *.js ]] && uglifyjs .....
    [[ $filename == *.less ]] && lessc bootstrap.less
done

You don't really need to worry about the internals but I just can't get the filename back into a bash scope. inotifywait echos out the filename (with help from the format argument) but how do I capture that and use it later on (in my case, as $filename)?


If you want a simple, short test harness:

touch testfile
while inotifywait testfile do; echo "..."; done

And then you can just run touch testfile when you want to trigger it.

Oli
  • 299,380

2 Answers2

23

The cleaner way is illustrated in this blog entry:

inotifywait -m -r -q --format '%w' bootstrap/ | while read FILE
do
  echo "something happened on path $FILE"
done
Oli
  • 299,380
3

Turns out I just needed to restructure the while so that I captured the output of inotifywait:

while true; do
    echo "something happened"
    filename=$(inotifywait -r -q --format %w bootstrap/)
    [[ $filename == *.js ]] && uglifyjs .....
    [[ $filename == *.less ]] && lessc bootstrap.less
done

I'm still curious to know if there are cleaner ways of doing this.

Oli
  • 299,380