Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active December 10, 2015 21:08
Show Gist options
  • Save mjf/4493097 to your computer and use it in GitHub Desktop.
Save mjf/4493097 to your computer and use it in GitHub Desktop.
IfChanged - returns 0 if file changed, 127 otherwise
#! /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
@mjf
Copy link
Author

mjf commented Jan 9, 2013

Use it as simple as follows:

:ifchanged test && echo CHANGED
Error: realpath test failed
:touch test
:ifchanged test && echo CHANGED
:ifchanged test && echo CHANGED
:echo TEST > test
:ifchanged test && echo CHANGED
CHANGED
:ifchanged test && echo CHANGED

Enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment