bash version check

bash version check

Postby hbuhrmester » 21.11.2022, 19:47

A recent patch from Eduardo de Oliveira for the file 30-check-needed-applications.bash included the comparison:

Code: Select all
if ((BASH_VERSINFO[0] <= 4))
then
    if ((BASH_VERSINFO[1] <= 2))
    then
        log_error_message "You need at least bash-4.2 to run this script"
        missing_binaries="$(( missing_binaries + 1 ))"
    fi
fi


but this doesn't work as expected:

  • If the bash-version is 4.2.x then we get the error message "You need at least bash-4.2 to run this script".
  • If the first version number is lower than 4, and the second version number is greater than 2, then we do not get any error message. This can be shown with versions numbers like 3.3.x.


For example, this script:

Code: Select all
#!/bin/bash

VERSINFO=("4" "2" "0")

if ((VERSINFO[0] <= 4))
then
    if ((VERSINFO[1] <= 2))
    then
        echo "You need at least bash-4.2 to run this script"
    fi
fi

exit 0


prints the error message "You need at least bash-4.2 to run this script", although this version should be sufficient.

This script:

Code: Select all
#!/bin/bash

VERSINFO=("3" "3" "0")

if ((VERSINFO[0] <= 4))
then
    if ((VERSINFO[1] <= 2))
    then
        echo "You need at least bash-4.2 to run this script"
    fi
fi

exit 0


does not print any error message.

Fortunately, there never was a bash version 3.3; but I still think, this version check is rather flawed.

Greetings,
Hartmut
hbuhrmester
 
Posts: 525
Joined: 11.10.2013, 20:59

Re: bash version check

Postby aker » 22.11.2022, 02:45

Ich werde das auf „Major < 4“ und „Major = 4 && Minor < 2“ ändern. So viel Bash kann ich noch.
Die vielen 24h- und Nachtdienste machen mich aktuell etwas mürbe…
Wer Rechtschreibfehler findet, darf sie behalten oder an den Meistbietenden versteigern. / Everybody finding a misspelling is allowed to keep or sell it.
aker

WSUS Offline Update „Community Edition“
https://gitlab.com/wsusoffline/wsusoffline/-/releases
aker
 
Posts: 3999
Joined: 02.03.2011, 15:32

Re: bash version check

Postby aker » 22.11.2022, 08:05

Got it.
Code: Select all
    if ((BASH_VERSINFO[0] < 4))
    then
        log_error_message "You need at least bash-4.2 to run this script"
        missing_binaries="$(( missing_binaries + 1 ))"
    fi
    if ((BASH_VERSINFO[0] -eq 4))
    then
        if ((BASH_VERSINFO[1] < 2))
        then
            log_error_message "You need at least bash-4.2 to run this script"
            missing_binaries="$(( missing_binaries + 1 ))"
        fi
    fi
Wer Rechtschreibfehler findet, darf sie behalten oder an den Meistbietenden versteigern. / Everybody finding a misspelling is allowed to keep or sell it.
aker

WSUS Offline Update „Community Edition“
https://gitlab.com/wsusoffline/wsusoffline/-/releases
aker
 
Posts: 3999
Joined: 02.03.2011, 15:32

Re: bash version check

Postby hbuhrmester » 22.11.2022, 19:49

if ((BASH_VERSINFO[0] -eq 4))


Die bash macht Unterschiede in den Vergleichen:

Code: Select all
if [ ... ]; then

if [[ ... ]]; then

if (( ... )); then


Einfache und doppelte eckige Klammern funktionieren ähnlich und werden vor allem für Textvergleiche genutzt.

Der eingebaute Befehl "[" funktioniert dabei genauso wie der externe Befehl "/bin/[" oder "/bin/test". Eingebaute Befehle sind nur etwas schneller.

Die doppelten eckigen Klammern "[[" gelten als ein "reserviertes Schlüsselwort". Das hat Auswirkungen auf die Fehlerbehandlung und das Word Splitting an Leerzeichen.

Die doppelten runden Klammern "((" werden für arithmetische Ausdrücke und Vergleiche verwendet, und man kann (und muss) darin normale mathematische Operatoren verwenden:

Code: Select all
if ((BASH_VERSINFO[0] == 4))


Referenz: https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html


Der Operator -eq verursacht in diesem Kontext nur einen Fehler und einen Abbruch des Skripts:

Code: Select all
$ ./update-generator.bash
Info: Starting update-generator.bash 1.22-ESR (2022-01-30)
Info: Command line: ./update-generator.bash
Info: Running on WSUS Offline Update, Community Edition 11.9.12
(b74)
Info: CreationDate of the update catalog file:
2022-10-11T01:11:14Z
Info: Repository last updated on 2022-11-05

Info: Checking needed applications...
./common-tasks/30-check-needed-applications.bash: line 100: eq: unbound variable
Keeping temporary files for debugging...
Exiting update-generator.bash (error code 1)...



Die Meldung "unbound variable" bedeutet, dass eq nun als ein Variablenname interpretiert wurde, der ganze Operator -eq also als "minus eq".


Das Skript syntax-check.bash wurde speziell dazu entwickelt, die Syntax aller Skripte im Ordner wsusoffline/sh zu überprüfen. Es macht einen kurzen Test mit der bash selber und dann einen gründlichen Test mit shellcheck:

Code: Select all
$ ./syntax-check.bash
Syntax check with bash...
Syntax check with shellcheck...
./30-check-needed-applications.bash:100:8: error: Shells disambiguate (( differently or not at all. For subshell, add spaces around ( . For ((, fix parsing errors. [SC1105]
./30-check-needed-applications.bash:100:9: warning: (..) is a subshell. Did you mean [ .. ], a test expression? [SC2205]
./30-check-needed-applications.bash:100:11: warning: This is a glob used as a command name. Was it supposed to be in ${..}, array, or is it missing quoting? [SC2211]
./30-check-needed-applications.bash:100:28: error: In arithmetic contexts, use == instead of -eq [SC1106]


Referenz: https://www.shellcheck.net/

Viele Grüße,
Hartmut
hbuhrmester
 
Posts: 525
Joined: 11.10.2013, 20:59

Re: bash version check

Postby aker » 22.11.2022, 23:35

Jetzt sollte es stimmen.

Vielen Dank
Wer Rechtschreibfehler findet, darf sie behalten oder an den Meistbietenden versteigern. / Everybody finding a misspelling is allowed to keep or sell it.
aker

WSUS Offline Update „Community Edition“
https://gitlab.com/wsusoffline/wsusoffline/-/releases
aker
 
Posts: 3999
Joined: 02.03.2011, 15:32


Return to Linux

Who is online

Users browsing this forum: No registered users and 151 guests