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
.
CodePudding user response:
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
CodePudding user response:
In the case that new
represents the latest version then you might just check for 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