Skip to content

Instantly share code, notes, and snippets.

@AriRexouium
Last active January 28, 2024 18:30
Show Gist options
  • Save AriRexouium/4a1277a9e8405a5b357778f6ff0764ba to your computer and use it in GitHub Desktop.
Save AriRexouium/4a1277a9e8405a5b357778f6ff0764ba to your computer and use it in GitHub Desktop.
Self Updating Github Gist Shellscript
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
set -euo pipefail
# Version 2024/01/28 - Changelog: https://gist.github.com/AriRexouium/4a1277a9e8405a5b357778f6ff0764ba
# This script checks for updates to a GitHub Gist and automatically updates if a new version is available.
#
# Usage: ./autoupdate.sh
#
# License:
# This script is licensed under the GNU General Public License v3.0 or later.
# For more information, see https://www.gnu.org/licenses/gpl-3.0.html
# Copyright (C) 2023 Jarrett Aiken <jarret.aiken@achl.fr> (https://achl.fr)
GITHUB_USERNAME="AriRexouium"; GITHUB_GIST="4a1277a9e8405a5b357778f6ff0764ba"
UPDATE_TIME_FILE="${0}.update"
if [ -f "${UPDATE_TIME_FILE}" ]; then
UPDATE_TIME=$(cat "${UPDATE_TIME_FILE}")
else
UPDATE_TIME=0
fi
CURRENT_TIME=$(date +%s); COOLDOWN_TIME=86400
if [ $((CURRENT_TIME - UPDATE_TIME)) -ge ${COOLDOWN_TIME} ]; then
CURRENT_VERSION=$(cat "${0}"); LATEST_VERSION=$(curl -s https://gist.githubusercontent.com/${GITHUB_USERNAME}/${GITHUB_GIST}/raw)
if [ "${CURRENT_VERSION}" != "${LATEST_VERSION}" ]; then
echo "${LATEST_VERSION}" > "${0}"
chmod +x "${0}"
echo "${CURRENT_TIME}" > "${UPDATE_TIME_FILE}"
exec "./${0}" "${@}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment