Skip to content

Instantly share code, notes, and snippets.

@ammarnajjar
Created July 24, 2016 10:40
Show Gist options
  • Save ammarnajjar/2d5fe1b3d9e9dc0fe5a35fa33da0da2f to your computer and use it in GitHub Desktop.
Save ammarnajjar/2d5fe1b3d9e9dc0fe5a35fa33da0da2f to your computer and use it in GitHub Desktop.
#!/bin/bash
# File: watchit.sh
# Author: Ammar Najjar <najjarammar@gmail.com>
# Description: watches for a file changes and react to that change
# Last Modified: July 05, 2016
function usage() {
echo "watches for a file changes and react to that change"
echo
echo "Usage:"
echo " bash $0 [option]"
echo " Options:"
echo " -h --help : Show this help messaage."
echo " -c --command : use custom command"
echo " -p --pandoc : create pdf using pandoc"
echo " -l --latex : create pdf using pdflatex"
echo " -m --make : make the project"
echo " -mf --make-pdf : make the project, update mupdf"
}
git_root=`git rev-parse --show-toplevel`
pdf_viewer='mupdf-x11'
function pandoc_cmd() {
if [[ -f $f ]]
then
filename=$(basename $f)
cd $(dirname $f)
fi
pandoc $filename -o "${filename%.*}.pdf"
killall -HUP $pdf_viewer
}
function tex_cmd() {
if [[ -f $f ]]
then
filename=$(basename $f)
cd $(dirname $f)
fi
pdflatex $filename -o "${filename%.*}.pdf"
killall -HUP $pdf_viewer
}
function make_cmd() {
if [[ -f $f ]]
then
cd $(dirname $f)
fi
if [[ -d $git_root ]]
then
cd $git_root
make
else
make
fi
}
function make_pdf() {
make_cmd
killall -HUP $pdf_viewer
}
while (inotifywait -r -e close_write,create,moved_to,modify .)
do
if [ "$f" = $1 ]; then
echo "watching: $f"
else
case $2 in
-h|--help)
usage
;;
-c|--command)
if [[ ! $3 ]]
then
usage
else
$3
fi
;;
-mf|--make-pdf)
make_pdf
;;
-m|--make)
make_cmd
;;
-p|--pandoc)
pandoc_cmd
;;
-l|--latex)
tex_cmd
;;
*)
echo "$*"
echo "Invalid arguments"
echo "Run with -h or --help to see the correct usage"
;;
esac
fi
done
# vim: set ft=sh ts=4 sw=4 et ai :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment