Skip to content

Instantly share code, notes, and snippets.

@Cat7373
Created February 26, 2018 05:52
Show Gist options
  • Save Cat7373/a87c55e24db5640dd120cac6bc73e5ba to your computer and use it in GitHub Desktop.
Save Cat7373/a87c55e24db5640dd120cac6bc73e5ba to your computer and use it in GitHub Desktop.
self update shell
#!/bin/sh
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
SCRIPTNAME="$0"
ARGS="$@"
BRANCH="master"
self_update() {
cd $SCRIPTPATH
git fetch
[ -n $(git diff --name-only origin/$BRANCH | grep $SCRIPTNAME) ] && {
echo "Found a new version of me, updating myself..."
git pull --force
git checkout $BRANCH
git pull --force
echo "Running the new version..."
exec "$SCRIPTNAME" "$@"
# Now exit this old instance
exit 1
}
echo "Already the latest version."
}
main() {
echo "Running"
}
self_update
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment