Skip to content

Instantly share code, notes, and snippets.

@unixmonkey
Created May 3, 2010 22:40
Show Gist options
  • Save unixmonkey/388675 to your computer and use it in GitHub Desktop.
Save unixmonkey/388675 to your computer and use it in GitHub Desktop.
#!/bin/bash
### SELF-UPDATING BASH SCRIPT
### Checks for update online, if not found, runs itself
# CONFIG: SET UPDATE FILE
UPDATE_FILE="update.sh"
UPDATE_URL="http://example.com/${UPDATE_FILE}"
### UPDATING CODE
THIS_FILE=$0
ARGUMENT=$1
function get_update {
echo "checking for update"
if [ ! -e $UPDATE_FILE ]; then
echo "update not found; attempting to download update"
wget $UPDATE_URL
fi
echo "checking again (in case download failed)"
if [ ! -e $UPDATE_FILE ]; then
echo "update still not found; copying current file to ${UPDATE_FILE}"
cp $THIS_FILE $UPDATE_FILE
fi
}
function execute_update {
echo "executing updated file"
chmod +x $UPDATE_FILE
./$UPDATE_FILE --run # must invoke with --run
}
function replace_current_file_with_update {
echo "overwriting current file with update file"
mv -f $UPDATE_FILE $THIS_FILE
}
function update {
# if the currently running script is not invoked with --run,
# then it is not the updated one
if [ "${ARGUMENT}" != "--run" ]; then
get_update
execute_update
replace_current_file_with_update
exit 0 # must exit, or script will recursively call itself for all eternity
fi
}
update # make the magic happen
### END UP UPDATING CODE: REST OF SCRIPT GOES BELOW THIS LINE
echo "RUNNING ACTUAL CODE NOW"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment