From 5a97d8d46621fbb51c25a087eae55af33cbf8aa8 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Fri, 2 Aug 2024 23:27:36 -0300 Subject: Feat: adds inotifier script --- inotifier | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 inotifier diff --git a/inotifier b/inotifier new file mode 100755 index 0000000..7c3c3be --- /dev/null +++ b/inotifier @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# +# Run commands when a folder content changes. +# + +# 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 [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 -- cgit v1.2.3