Last active
December 10, 2015 21:08
-
-
Save mjf/4493097 to your computer and use it in GitHub Desktop.
IfChanged - returns 0 if file changed, 127 otherwise
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 | |
# IfChanged - returns 0 if file changed, 127 otherwise | |
# Copyright (C) 2013 Matous J. Fialka, <http://mjf.cz/> | |
# Released under the terms of The MIT License | |
fpath="$(realpath 2>/dev/null ""$1"")" || { | |
echo "Error: realpath $1 failed" 1>&2 | |
exit 1 | |
} | |
dpath="$HOME/.ifchanged/${fpath%/*}" | |
mkdir -p "$dpath" &>/dev/null || { | |
echo "Error: mkdir -p $dpath failed" 1>&2 | |
exit 2 | |
} | |
if [ -e "$dpath/$1" ] | |
then | |
mv "$dpath/$1" "$dpath/$1.last" &>/dev/null || { | |
echo "Error: mv $dpath/$1 $dpath/$1.last failed" 1>&2 | |
exit 3 | |
} | |
else | |
sha1sum "$fpath" 2>/dev/null 1>"$dpath/$1" || { | |
echo "Error: sha1sum $fpath > $dpath/$1 failed" 1>&2 | |
exit 4 | |
} | |
fi | |
sha1sum "$fpath" 2>/dev/null 1>"$dpath/$1" || { | |
echo "Error: sha1sum $fpath > $dpath/$1 failed" 1>&2 | |
exit 5 | |
} | |
diff -q "$dpath/$1" "$dpath/$1.last" &>/dev/null || exit 0 | |
exit 127 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it as simple as follows:
Enjoy.