4

I need a script to check if yad (and other programs) version number is >= to a specific number. For example I have:

$ yad --version
0.40.0 (GTK+ 3.24.8)

$ gedit --version
gedit - Version 3.32.0

$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
  • For yad new features are added between Ubuntu 16.04 and 19.04
  • For gedit the ability to pass Window geometry is lost in newer versions
  • bash complicates tests as the version number is in the middle of the first line.

An environment variable will not exist for all programs like bash has:

$ echo $BASH_VERSION
5.0.3(1)-release
K7AAY
  • 17,202

3 Answers3

5

You might want to try GNU sort's -V (--version-sort), along with -C (--check=quiet):

$ echo $BASH_VERSION
4.4.20(1)-release

then to return 0 (true) if the version is at least the given one and 1 (false) otherwise:

$ printf '%s\n%s\n' "$BASH_VERSION" "4.3" | sort -rVC ; echo $?
0

$ printf '%s\n%s\n' "$BASH_VERSION" "4.4.20(2)" | sort -rVC ; echo $?
1
steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • 2
    Apologies I posted two similar questions about the same time. This answer is perfect for the other question: https://askubuntu.com/questions/916976/check-if-bash-version-is-given-version-number – WinEunuuchs2Unix Aug 05 '19 at 17:23
1

I developed a script that draws on answers in Stack Overflow. One of those answers led to a Dell Employee writing version number comparisons in 2004 for the DKMS application.

Sample Tests

$ testver yad 0.40.0; echo $?
0
$ testver yad 0.41.0; echo $?
1
$ testver bash 5.0.3; echo $?
0
$ testver bash 5.0.4; echo $?
1
$ testver gedit 3.32.0; echo $?
0
$ testver gedit 4.32.0; echo $?
1
$ testver iwconfig 30; echo $?
0
$ testver iwconfig 31; echo $?
1

Real life application

if testver gnome-shell 3.32.0 ; then
    # returns 0 version 3.32.0 and greater geometry not supported.
    nohup gedit $@ &>/dev/null &
else
    # returns 1 version less than 3.32.0 so geometry supported.
    nohup gedit -g 1300x840+4565+2345 $@ &>/dev/null &
fi

The code

The bash script below needs to be marked as executable using the command chmod a+x script-name. I'm using the name /usr/local/bin/testver:

#!/bin/bash

# NAME: testver
# PATH: /usr/local/bin
# DESC: Test a program's version number >= to passed version number
# DATE: May 21, 2017. Modified August 5, 2019.

# CALL: testver Program Version

# PARM: 1. Program - validated to be a command
#       2. Version - validated to be numberic

# NOTE: Extracting version number perl one-liner found here:
#       http://stackoverflow.com/questions/16817646/extract-version-number-from-a-string

#       Comparing two version numbers code found here:
#       http://stackoverflow.com/questions/4023830/how-compare-two-strings-in-dot-separated-version-format-in-bash

# Map parameters to coder-friendly names.
Program="$1"
Version="$2"

# Program name must be a valid command.
command -v $Program >/dev/null 2>&1 || { echo "Command: $Program not found. Check spelling."; exit 99; }

# Passed version number must be valid format.
if ! [[ $Version =~ ^([0-9]+\.?)+$ ]]; then
    echo "Version number: $Version has invalid format. Aborting.";
    exit 99
fi

InstalledVersion=$( "$Program" --version | perl -pe '($_)=/([0-9]+([.][0-9]+)+)/' )

# Perl command doesn't work for non-decimal version numbers
[[ "$InstalledVersion" == "" ]] && 
     InstalledVersion=$( "$Program" --version | head -n1 | tr -dc '0-9')

if [[ $InstalledVersion =~ ^([0-9]+\.?)+$ ]]; then
    l=(${InstalledVersion//./ })
    r=(${Version//./ })
    s=${#l[@]}
    [[ ${#r[@]} -gt ${#l[@]} ]] && s=${#r[@]}

    for i in $(seq 0 $((s - 1))); do
        # echo "Installed ${l[$i]} -gt Test ${r[$i]}?"
        [[ ${l[$i]} -gt ${r[$i]} ]] && exit 0 # Installed version > test version.
        [[ ${l[$i]} -lt ${r[$i]} ]] && exit 1 # Installed version < test version.
    done

    exit 0 # Installed version = test version.
else
    echo "Invalid version number: $InstalledVersion found for command: $Program"
    exit 99
fi

echo "testver - Unreachable code has been reached!"
exit 255
1

You can use dpkg --compare-versions. Usage example:

$ dpkg --compare-versions 4.0 lt 5.0 && echo true
true

This returns "true" because version 4.0 is less than ("lt") version 5.0.

On the other hand the following does not return anything:

$ dpkg --compare-versions 4.0 gt 5.0 && echo true

This is because version 4.0 is not greater ("gt") than 5.0.

Comparison operators for dpkg --compare-versions are:

  • lt le eq ne ge gt (treat empty version as earlier than any version);
  • lt-nl le-nl ge-nl gt-nl (treat empty version as later than any version);
  • < << <= = >= >> > (only for compatibility with control file syntax).
Logix
  • 2,230