Skip to content

Instantly share code, notes, and snippets.

@mikestreety
Created December 6, 2013 18:53
Show Gist options
  • Save mikestreety/7830198 to your computer and use it in GitHub Desktop.
Save mikestreety/7830198 to your computer and use it in GitHub Desktop.
Using inotify to detect changes in a file. This case is used for less files to compile them
if which inotifywait >/dev/null; then # Checks to see if inotify tools exists
folder=`pwd`; # This is where it searches - pwd will run that command and run the rest based on your current folder
while true;
do
N=`find $folder -name "*.less"`; # Currently searches for all .less files in the folder above using a standard find command
if [[ -z $N ]]; then # If there is nothing found then it exits
echo There are no LESS files to be found - maybe cd to where there is some.
exit;
else
fileout=`inotifywait -qe modify $N`; # Gets the name of the file that was modified
newfileout=${fileout/ MODIFY/}; # get the path of the modified file
stripped_file=${newfileout/$folder\//} # remove the pwd from the path
echo -e "\e[90mFound changes in: $stripped_file - compiling..."; # Echo what its found
for f in $newfileout; # For each file in the modified string (normally one)
do
n=${f/css\/less/css} # replace css/less in the path with css
compile=`lessc --verbose --yui-compress $f ${n%.*}.css`; # run the lessc command
if [ "$compile" ]
then
echo -e "\e[32mCompiled\e[0m: $stripped_file - at `date +"%T"`."; # echo that its compiled
fi
done;
fi
done;
else
echo Please install inotify-tools
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment