Last active
October 22, 2024 21:44
-
-
Save marc0x71/d89f023df83e74dddc6199eed8adb439 to your computer and use it in GitHub Desktop.
File watcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
normal=$(tput sgr0) | |
yellow=$(tput setaf 3) | |
green=$(tput setaf 2) | |
FOLDER="." | |
PATTERN=".*" | |
COMMAND="ls" | |
spinner() { | |
SPIN='|/-\' | |
echo "" | |
while true | |
do | |
TMP=${SPIN#?} | |
tput sc | |
printf "${yellow}waiting...${normal} [${yellow}%c${normal}] " "$SPIN" | |
SPIN=$TMP${SPIN%"$TMP"} | |
sleep 0.5 | |
tput rc | |
done | |
} | |
while getopts 'f:p:x:h' opt | |
do | |
case "$opt" in | |
f) | |
FOLDER=$OPTARG | |
;; | |
p) | |
PATTERN=$OPTARG | |
;; | |
x) | |
COMMAND=$OPTARG | |
;; | |
h) | |
echo "Usage: $(basename $0) [-f FOLDER] [-p PATTERN] [-x COMMAND]" | |
exit 1 | |
;; | |
esac | |
done | |
if ! hash inotifywait 2>&1 >/dev/null | |
then | |
echo "inotifywait not found" | |
exit 1 | |
fi | |
clear | |
while true | |
do | |
# eval command | |
clear | |
printf "${green}${COMMAND}${normal}\n" | |
eval $COMMAND | |
# start spinner | |
spinner & | |
pid=$! | |
# waiting for an event | |
inotifywait -q -r -P -I -e modify,delete $FOLDER --include "${PATTERN}" >/dev/null 2>&1 | |
# kill spinner | |
kill $pid | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment