1

i am creating a script that will help updating the GoLang compile binary in a GNU/Linux system.

but it fail

#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# set -e
# ==============================================================================
# title             : Semi Automatic Update GoLang
# description       : Script for Install, setup path, permission and update golang
# author            : Walddys Emmanuel Dorrejo Céspedes
# usage             : bash up_install.sh
# notes             : Execute this Script will ask sudo password
# dependencies      : wget awk sed curl tar
# ==============================================================================

## Get Golang Versions from repository
declare -a go_jversion go_sversion
readarray -t go_jversion < <(curl -s https://go.googlesource.com/go/+refs?format=JSON | grep -Eo "go[0-9]\.[^\"]+" | sort -V)

## Delete go_versions RC and Beta from the pool.
for i in "${!go_jversion[@]}"; do
    if [[ "${go_jversion[i]}" =~ (rc|beta) ]]; then
        unset "go_jversion[i]"
    fi
done

unset go_sversion # Not allow to grow indefinitely the pool when re-execute the scripts
for i in "${!go_jversion[@]}"; do
    set -vx
    ## Create an array of the stables versions (Those versions that repeat more than or equal to 2 are stables)
    # if [[ "${go_jversion[i]}" == "${go_jversion[i + 1]}" ]] && [[ "${go_sversion[i - 1]}" != "${go_jversion[i + 1]}" ]]; then
    #     go_sversion+=("${go_jversion[i]}")
    # fi

In this section i am comparing major version + minimum version, to exclude the patch version of the array, but the condition after the "&&", the array "${go_sversion[$i -1]}" is expanding null in each cycle of the loop, when i am assigning a value in a cycle before.

    ## Create an array of the stables versions (Those versions that repeat more than or equal to 2 are stables) (second version)
    if [[ "${go_jversion[$i]}" == "${go_jversion[$i + 1]}" && "${go_sversion[$i - 1]}" != "${go_jversion[$i]}" ]]; then
        go_sversion+=("${go_jversion[$i]}")
        echo "${!go_sversion[$i]}"
    fi
    set +vx
done

echo "${go_sversion[@]}"
echo "${!go_sversion[@]}"

My issue is in the section where "${go_sversion[$i -1]}", why is not expanding?

assign value to "${go_sversion[$i -1]}" the value display in the next cycle of the loop

  • 1
    before each of your `for` loops add `typeset -p go_jversion` to show the contents of the array; with the 2nd set of output now step through the 2nd `for` loop and see what you get for each tuple of `$i-1`, `$i` and `$i+1` – markp-fuso Feb 14 '23 at 14:40

1 Answers1

5

Arrays in bash are allowed to be sparse, meaning their indices are not required to be strictly sequential. For example:

arr=(1 2 3)
echo "${arr[@]}" # prints 1 2 3
echo "${!arr[@]}" # prints 0 1 2
unset arr\[1\]
echo "${arr[@]}" # prints 1 3
echo "${!arr[@]}" # prints 0 2

When you unset the RC and Beta values you could be creating these types of gaps in your jversion array, but you're assigning to the sversion array sequentially. This means the indices do not align between the arrays.

If your jversion looks like my array above, you might put something into sversion[0] from jversion[0], then process jversion[2] and attempt to match it against sversion[1] which doesn't exist yet.

One simple way to de-sparsify the array is to reassign it:

go_jversion=( "${go_jversion[@]}" )

This will reassign the contents of the array to itself in sequential order without any gaps in the indices.

If this is unviable for some reason, you'll have to write code that is aware of the possible sparseness of the array. For example, instead of blinding looking at go_sversion[i-1] you could look at go_sversion[-1] which will always give you the last item in the array.

tjm3772
  • 2,346
  • 2
  • 10
  • Hello, thanks you very much, what way can i use to remove the index instead of the value?, so i dont have index unused. – Walddys E. Dorrejo Céspedes Feb 20 '23 at 16:57
  • 1
    I'll add an example to the answer. – tjm3772 Feb 20 '23 at 17:36
  • Can't edit the previous comment. Edit: ------- Found the solution and a Theoric of how array work that I ignored: https://stackoverflow.com/questions/16860877/remove-an-element-from-a-bash-array Thanks to you help, I try working in other way and found the previous post, of how to do it. Thank you very much – Walddys E. Dorrejo Céspedes Feb 20 '23 at 18:04