1

I'm trying to add the ability for my ArchLinux installer script to check if it's update-to-date based on rather it matches (or doesn't match) the version number that's on gitlab. The primary script that runs the installer (and all of the numbered script files) is the aalis.sh script, it basically goes and runs the other files together. The version numbering would be something like 1.2.3 (major.minor.patch).

Basically, whenever I make any changes to the script, I will change the script's version number of gitlab; and I want the script itself to be able to detect that its version number doesn't the match the one on github (for cases where someone has an outdated version of the script and try to run it); and automatically update itself using git fetch origin master then rerun itself using the updated contents.

4
  • Don't do this. It will really annoy anyone who doesn't want to update the script right now, or who depends on the behaviour (including bugs) of previous versions of the script, or who doesn't want the risk of your new version being buggy or your updater being buggy, or who have specific testing and deployment procedures for new code. Forced upgrades are evil, even if you think you know best...even if you actually do know best.
    – cas
    Jan 2, 2022 at 1:45
  • I won't have it where it just automatically updates right off the bat, but it will check for any and ask the user if they would like to update (and possibly show a summary of changes), and if they wish to not update, then it won't update and will continue on running the rest of the script
    – Nova Leary
    Jan 2, 2022 at 3:12
  • This is better, but even this will cause problems, especially with automation and wrapper scripts - e.g. running it from cron or as part of a script. Just printing a warning message is enough. More than enough, really. If you think you must ask annoying questions, at least add a "--do-not-ask-annoying-questions" option.
    – cas
    Jan 2, 2022 at 4:48
  • Alrighty, I'm going to just have it where it simply warns the user the script is out of date!
    – Nova Leary
    Jan 2, 2022 at 6:48

2 Answers 2

0

Place everything inside functions before updating script. This make sure all code are loaded before the script's contents are replaced.

function some_function {
    ...
}

function main {
    ...
}

main "$@"

If you can separate your version in another file like version.sh. That would be even much safer.

2
  • I actually do have in mind creating the functions within another script, but I'm not sure how I can make the script compare the local file to the remote file online (and compare the version numbers within each file)
    – Nova Leary
    Jan 2, 2022 at 3:14
  • You have to ask a more specific question.
    – konsolebox
    Jan 2, 2022 at 7:08
0

I figured it out! I found this answer for retrieving the file from the script's online repo, taking the curl command and changed the URL to simply retrieve the VERSION.txt file (which will only contain the version number, such as 2.4.0 and found this answer for the actual version comparison functionality. After combining the two answers (and changing the output for the test_compare_versions script, my result's like this

# Answer from https://stackoverflow.com/a/49351294/17812185 by Maxxim
# Compare two version strings [$1: version string 1 (v1), $2: version string 2 (v2)]
# Return values:
#   0: v1 == v2
#   1: v1 > v2
#   2: v1 < v2
# Based on: https://stackoverflow.com/a/4025065 by Dennis Williamson
function compare_versions() {

    # Trivial v1 == v2 test based on string comparison
    [[ "$1" == "$2" ]] && return 0

    # Local variables
    local regex="^(.*)-r([0-9]*)$" va1=() vr1=0 va2=() vr2=0 len i IFS="."

    # Split version strings into arrays, extract trailing revisions
    if [[ "$1" =~ ${regex} ]]; then
        va1=(${BASH_REMATCH[1]})
        [[ -n "${BASH_REMATCH[2]}" ]] && vr1=${BASH_REMATCH[2]}
    else
        va1=($1)
    fi
    if [[ "$2" =~ ${regex} ]]; then
        va2=(${BASH_REMATCH[1]})
        [[ -n "${BASH_REMATCH[2]}" ]] && vr2=${BASH_REMATCH[2]}
    else
        va2=($2)
    fi

    # Bring va1 and va2 to same length by filling empty fields with zeros
    (( ${#va1[@]} > ${#va2[@]} )) && len=${#va1[@]} || len=${#va2[@]}
    for ((i=0; i < len; ++i)); do
        [[ -z "${va1[i]}" ]] && va1[i]="0"
        [[ -z "${va2[i]}" ]] && va2[i]="0"
    done

    # Append revisions, increment length
    va1+=($vr1)
    va2+=($vr2)
    len=$((len+1))

    # *** DEBUG ***
    echo "TEST: '${va1[@]} (?) ${va2[@]}'"

    # Compare version elements, check if v1 > v2 or v1 < v2
    for ((i=0; i < len; ++i)); do
        if (( 10#${va1[i]} > 10#${va2[i]} )); then
            return 1
        elif (( 10#${va1[i]} < 10#${va2[i]} )); then
            return 2
        fi
    done

    # All elements are equal, thus v1 == v2
    return 0
}

# Test compare_versions [$1: version string 1, $2: version string 2, $3: expected result]
function test_compare_versions() {
    compare_versions "$1" "$2"
    case $? in
        0) op="==" ;;
        1) op=">" ;;
        2) op="<" ;;
    esac

    if [[ "$op" == "==" ]]; then
        output ${LIGHT_GREEN} "The script is update to date!"
    elif [[ "$op" == "<" ]]; then
        output ${RED} "The script is out of date!!"
    elif [[ "$op" == ">" ]]; then
        output ${YELLOW} "The script is ahead of the release version, so this must be a release canidate or a beta build!"
    else
        banner ${RED} "SOMETHING HAS GONE WRONG, PLEASE REPORT THIS ERROR TO THE PROJECT'S GITLAB"
    fi

}


VERSION=1.2.0
ONLINE_VERSION=$(curl -s https://gitlab.com/NovaViper/testing-for-aalis-version-checker/-/raw/main/VERSION.txt)
test_compare_versions $VERSION $ONLINE_VERSION

5
  • I have a simpler one: stackoverflow.com/a/70546986/445221. And I replicated it to that question: stackoverflow.com/a/70555119/445221.
    – konsolebox
    Jan 2, 2022 at 10:05
  • There has been for years a script called bootinfoscript. Several forks over the years, and I do not think any are maintained anymore. But it has update & download new versions options. github.com/arvidjaar/bootinfoscript I still use last version to document my system as well as Boot-Repair which requires a gui.
    – oldfred
    Jan 2, 2022 at 16:26
  • @konsolebox Ah cool! Does your answer support release candidate tags (like -rc?
    – Nova Leary
    Jan 2, 2022 at 17:44
  • @NovaLeary No it doesn't. It's built simple to only support integers. Tell me if you decide to open a question about it.
    – konsolebox
    Jan 2, 2022 at 17:54
  • @konsolebox Ah OK! Hm.. I might actually go with yours sense it's alot easier but I need to figure out the release candidates/beta tags. Most likely going to open a question about it later on today
    – Nova Leary
    Jan 2, 2022 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .