0

I have a small script in bash, I want to compare an old firmware version with a new one.

For example:

old fw: 2.9.7
new fw: 2.13.3

But when I run the script, it detects that 2.9 is higher than 2.13 and it doesn't update.

This is my script

#!/bin/bash
old=2.9.7
new=2.13.3

if [[ $old < $new ]];then
echo "Run the updates";else
echo "Does not need to update";fi

I get Does not need to update.

Cyrus
  • 77,979
  • 13
  • 71
  • 125
  • 1
    but those aren't numbers!? Do you mean semantic versions? – Christian Fritz Apr 01 '22 at 19:36
  • Does ["How to compare two strings in dot separated version format in Bash?"](https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash) answer your question? – Gordon Davisson Apr 01 '22 at 19:57

2 Answers2

2

With GNU sort and bash:

old=2.9.7; new=2.13.3

if [[ $(sort -V <(printf "%s\n%s\n" $old $new) | tail -n 1) == $new ]] ; then
  echo "newer"
else
  echo "older"
fi

From man sort:

-V: natural sort of (version) numbers within text

Cyrus
  • 77,979
  • 13
  • 71
  • 125
0

If new represents the latest version and you want to know if you should run an update, then you might just check the string equality:

old=2.9.7 new=2.13.3

if [[ "$old" != "$new" ]]
then
    echo "Run the updates"
else
    echo "Does not need to update"
fi
Fravadona
  • 5,892
  • 14
  • 26