scripts

My personal collection of scripts
git clone git://src.gearsix.net/scripts
Log | Files | Refs | Atom | README | LICENSE

onfilech.sh (566B)


      1 #!/usr/bin/env sh
      2 # on file change
      3 # description: when a file ($1) modified time is updated, perform $2..
      4 # dependencies: stat
      5 # e.g.$ onfilech draft-plans.txt cp draft-plans.txt new-plans.txt
      6 
      7 if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
      8 	echo "Usage: onfilech FILE COMMAND..."
      9 	echo ""
     10 	echo "run COMMAND every time a change is made to FILE"
     11 	exit
     12 fi
     13 
     14 file=$1; shift
     15 cmd=$*
     16 
     17 while true; do
     18 	mod=$(stat "$file");
     19 	while [ "$mod" != false ]; do
     20 		upd=$(stat "$file");
     21 		if [ "$upd" != "$mod" ]; then
     22 			$cmd; echo update; mod=false;
     23 		fi;
     24 		sleep 3;
     25 	done;
     26 done;