Bash: Compare software version

by admin on

A little piece of code to compare software versions in bash. I used it once to check whether a software was updated or not, then throw an alert, but since it is a general function, you can use it about anywhere.

All you have to do is to extract version numbers and then write this command:

Then you check the variable exit_code, using the following values:

  • 10: the two versions are equal
  • 11: version_1 is greater than version_2
  • 9: version_2 is greater than version_1

Very basically, this scripts recursively extracts the first  number before a dot in both variables, then check if one of the two numbers is greater. I didn’t change not updated anything from the source, even if it’s fairly old, for it seems to me that it’s well coded and it works out of the box, handling almost every situation.

SOURCES

Written by: admin

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *