aboutsummaryrefslogtreecommitdiff
path: root/inotifier
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2024-08-20 20:42:16 -0300
committerSilvio Rhatto <rhatto@riseup.net>2024-08-20 20:42:16 -0300
commitd50c3847192acbb3a5be68b67320fd1819fa81fb (patch)
treeaaa338f51b4ae09c18a5980d5952232d3bf51678 /inotifier
downloadutils-cli-d50c3847192acbb3a5be68b67320fd1819fa81fb.tar.gz
utils-cli-d50c3847192acbb3a5be68b67320fd1819fa81fb.tar.bz2
Initial import
Diffstat (limited to 'inotifier')
-rwxr-xr-xinotifier48
1 files changed, 48 insertions, 0 deletions
diff --git a/inotifier b/inotifier
new file mode 100755
index 0000000..c4d7098
--- /dev/null
+++ b/inotifier
@@ -0,0 +1,48 @@
+#!/usr/bin/env bash
+#
+# Run commands when a folder content changes.
+#
+# Alternatives:
+#
+# * https://github.com/quitesimpleorg/adhocify
+# * https://tracker.debian.org/pkg/inotify-hookable
+# * https://metacpan.org/dist/App-Inotify-Hookable
+
+# Parameters
+BASENAME="`basename $0`"
+WATCHED="$1"
+COMMAND="$2"
+
+# Listened inotify events
+#
+# See inotifywait(0) for the full list and description of all supported events.
+#
+# By default, exclude "access", "close", "close_write", "close_nowrite", "open"
+# from the list of events
+INOTIFY_EVENTS="-e modify -e attrib -e moved_to -e moved_from -e move -e move_self -e create -e delete -e delete_self -e unmount"
+
+# Check
+if [ -z "$COMMAND" ]; then
+ echo "usage: $BASENAME <folder> <command> [args]"
+ exit 1
+elif [ ! -e "$WATCHED" ]; then
+ echo "error: file or folder not found: $WATCHED"
+ exit 1
+fi
+
+# Shift
+shift 2
+
+# Normalize folder name
+if [ -d "$WATCHED" ]; then
+ WATCHED="`cd $WATCHED &> /dev/null && pwd`"
+fi
+
+# UX
+echo "Watching $WATCHED to exec \"$COMMAND $*\" upon changes..."
+
+# Dispatch
+# Excluding any .git folder from being watched
+while inotifywait $INOTIFY_EVENTS --exclude '.*.git.*' -r $WATCHED; do
+ $COMMAND $*
+done