Created
July 24, 2011 03:49
-
-
Save matthewjosephtaylor/1102205 to your computer and use it in GitHub Desktop.
Bash shell script that watches a file and executes a command if the file changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Shell script that watches a file and executes a command if the file changes. | |
# Matt Taylor | |
# http://blog.matthewjosephtaylor.com | |
# License: This is free and unencumbered software released into the public domain. | |
# Thanks to code/tips from some helpful souls on irc.freenode.net ##unix | |
# All errors are of course my own. :) | |
#BSD Based Unix (Mac OSX) | |
STAT_FILE_MOD_PARAM="%m" | |
#SVR4 Based Unix (Linux) | |
#STAT_FILE_MOD_PARAM="%y" | |
function usage { | |
echo "usage: $(basename $0) file command [command parameters ...]" | |
exit | |
} | |
if [ $# -lt "2" ] | |
then | |
usage | |
fi | |
THE_FILE=$1; shift | |
THE_COMMAND=$@; | |
function get_mod { | |
local result=$(stat -f "${STAT_FILE_MOD_PARAM}" ${THE_FILE}) | |
echo "$result" | |
} | |
last_mod=$(get_mod) | |
function test_modified { | |
local current_mod=$(get_mod) | |
if [ "$current_mod" != "$last_mod" ] | |
then | |
$THE_COMMAND | |
#echo $current_mod | |
#echo $last_mod | |
fi | |
last_mod=$current_mod | |
} | |
while true; do | |
sleep 1 | |
test_modified | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment