UNPKG

1.02 kBapplication/x-shView Raw
1#!/usr/bin/env bash
2# script: watch
3# author: Mike Smullin <mike@smullindesign.com>, Carlo Cancellieri <ccancellieri@gmail.com>
4# license: GPLv3
5# description:
6# watches the given path for changes
7# and executes a given command when changes occur
8# usage:
9# watch <path> <cmd...>
10#
11path=$1
12type sha1sum 2> /dev/null
13if [ $? -eq 0 ]; then
14 shasum=sha1sum
15else
16 shasum=shasum
17fi
18
19shift
20cmd=${*:1}
21sha=0
22update_sha() {
23 #sha=`ls -lR --time-style=full-iso $path | $shasum`
24 sha=`ls -lR $path | $shasum`
25}
26update_sha
27previous_sha=$sha
28build() {
29 echo -en "\n running -> ${cmd[@]}\n\n"
30 eval ${cmd[@]}
31 echo -en "\n--> resumed watching."
32}
33compare() {
34 update_sha
35 if [[ $sha != $previous_sha ]] ; then
36 echo -n "change detected,"
37 build
38 previous_sha=$sha
39 else
40 echo -n .
41 fi
42}
43#trap build SIGINT
44#trap exit SIGQUIT
45
46#echo -e "--> Press Ctrl+C to force build, Ctrl+\\ to exit."
47build
48echo -e "--> Press Ctrl+C to exit."
49echo -en "--> watching \"$path\"."
50while true; do
51 compare
52 sleep 1
53done