#!/bin/bash # hook into inotify to watch a file, and generate # diffs for changes in real-time # # requires inotify-tools (apt-get install inotify-tools) # # @author Filipe Dobreira usage() { echo "Usage:" echo " in-diff |help" exit 1 } # check arguments: if [ "$1" == "" ] || [ ! -f "$1" ] || [ "$1" == "help" ]; then usage fi # check dependencies: command -v inotifywait >/dev/null 2>&1 || { echo >&2 "inotifywait(1) not available, please install inotify-tools"; exit 1; } FILE="$1" SNAP=$(tempfile) # copy the initial snapshot to a temp file cp "$FILE" "$SNAP" watch () { # start an inotifywatch loop: while RES=$(inotifywait -q -e modify $FILE); do diff "$FILE" "$SNAP" -d cp "$FILE" "$SNAP" done } # Keep watching for changes, even if the file is deleted. while true; do if [ ! -f "$FILE" ]; then echo "" > "$SNAP" else watch fi done