Skip to content

Instantly share code, notes, and snippets.

@gaganjakhotiya
Last active February 4, 2018 11:06
Show Gist options
  • Save gaganjakhotiya/264f0195f1088c771cfd8532e49398c7 to your computer and use it in GitHub Desktop.
Save gaganjakhotiya/264f0195f1088c771cfd8532e49398c7 to your computer and use it in GitHub Desktop.
Bash CLI to watch a file and execute commands on file update

checkmod

Bash CLI to watch a file and execute commands on file update

Installation

  • Download the checkmod.sh file
  • Copy the checkmod.sh file and give permissions to execute
$ cp checkmod.sh /usr/bin/checkmod
$ chmod +x /usr/bin/checkmod

Example Usage

$ checkmod main.cpp "g++ main.cpp && eval ./a.out"

checkmod watch

Keep watching ;)

#!/bin/bash
TIMER=0.1
FILE=$1
CMD=$2
if [ ! -f "$FILE" ]; then
echo "File not found! Exiting watch mode."
exit
fi
LAST_TIME=`stat -c "%Y" $FILE`
declare -a LOADER=("|" "/" "—" "\\")
ok_bye () {
echo -ne "\r\033[0KShutting down watch! Bye.\n"
exit
}
check_and_eval () {
a=`stat -c "%Y" $FILE`
if [[ ! ("$a" -eq "$LAST_TIME") ]]; then
let LAST_TIME="$a"
b=`date -r $FILE`
echo -ne "\r\033[0K[UPDATE] $b\n"
eval "$CMD"
echo ""
fi
}
run_infy () {
index=0
while `true`
do
if [ "$index" -eq 3 ]; then
index=0
else
let index++
fi
echo -ne \\r ${LOADER[$index]} "Watching $FILE for changes\033[0K"
check_and_eval
sleep $TIMER
done
}
trap ok_bye SIGINT SIGTERM
run_infy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment