Skip to content

Instantly share code, notes, and snippets.

@elmarx
Created December 12, 2014 09:33
Show Gist options
  • Save elmarx/3a6c9d82455151b403b9 to your computer and use it in GitHub Desktop.
Save elmarx/3a6c9d82455151b403b9 to your computer and use it in GitHub Desktop.
shell-scripts that only executes itself if it changed since the previous run.
#!/bin/sh -
#===============================================================================
#
# FILE: am-i-changed.sh
#
# USAGE: ./am-i-changed.sh
#
# DESCRIPTION: simple snippet to test if the current file has changed, thus needs to be re-run
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Elmar Athmer ([email protected]),
# ORGANIZATION:
# CREATED: 12.12.2014 10:08
# REVISION: ---
#===============================================================================
set -o nounset
set -o errexit
FILE=/tmp/am-i-changed-status.md5
touch $FILE
LAST_HASH=$(cat $FILE)
CURRENT_HASH=$(cat $0 | md5sum | xargs)
if [ "${LAST_HASH}" = "${CURRENT_HASH}" ]; then
echo "nothing to do"
exit 0
else
echo "i did change, so continue running this script"
echo $CURRENT_HASH > $FILE
fi
echo "Hello World!"
@elmarx
Copy link
Author

elmarx commented Dec 12, 2014

$ ./am-i-changed.sh # first execution
i did change, so continue running this script
Hello World!
$ ./am-i-changed.sh # second execution
nothing to do

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