Skip to content

Instantly share code, notes, and snippets.

Last active July 7, 2023 04:37
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cubedtear/54434fc66439fc4e04e28bd658189701 to your computer and use it in GitHub Desktop.
Self-updating bash script
#!/usr/bin/env bash
rm -f
function update()
TMP_FILE=$(mktemp -p "" "")
curl -s -L "$SCRIPT_URL" > "$TMP_FILE"
NEW_VER=$(grep "^VERSION" "$TMP_FILE" | awk -F'[="]' '{print $3}')
if [ "$VERSION" \< "$NEW_VER" ]
printf "Updating script \e[31;1m%s\e[0m -> \e[32;1m%s\e[0m\n" "$VERSION" "$NEW_VER"
echo "cp \"$TMP_FILE\" \"$ABS_SCRIPT_PATH\"" >
echo "rm -f \"$TMP_FILE\"" >>
echo "echo Running script again: `basename ${BASH_SOURCE[@]}` $@" >>
echo "exec \"$ABS_SCRIPT_PATH\" \"$@\"" >>
chmod +x
chmod +x "$TMP_FILE"
rm -f "$TMP_FILE"
update "$@"
echo "$@"
Copy link

andreas-becker commented Nov 2, 2021

SCRIPT_DESCRIPTION is unused and could be removed
${BASH_SOURCE[@]} double quote array expansions to avoid re-splitting elements

Copy link

SCRIPT_DESCRIPTION is unused and could be removed

It is intended as a placeholder in order to remember to describe the scripts usage. A comment would be enough, but they are easily forgotten.

Copy link

hueyvle commented Jul 7, 2023

Thanks for a very good script.
I have a few suggestions tho.

  1. add a check for curl
    which curls > /dev/null 2>&1 || echo "curl not found"

  2. check for curl download failure and add logic to skip if there is no internet connection
    curl --connect-timeout 5 ....

  3. Your version comparison would fail: ie.
    "1.10.1" < "1.2.1" will return true, when it is not.
    There are a few technique to compare versions on the internet. just google it.

  4. is hardcoded too many times. puting it in a local variable would be better.

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