Version numbers are actually pretty tough to compare given that they're often not standard mathematical comparisons (this one for instance has two decimal points that makes bc
throw up).
Therefore I turn to a lesser-known option in sort
that can be used to sort version strings. Consider the following:
$ echo -e "v0.1.5\nv0.1.6-2-p343h3d3" | sort -V
v0.1.5
v0.1.6-2-p343h3d3
oli@bert:~$ echo -e "v0.1.5\nv0.1.4-2-p343h3d3" | sort -V
v0.1.4-2-p343h3d3
v0.1.5
This basically means we can sort the versions so the latest is on the last line. All we have to do then is a string comparison against the last line, inside an if
, or a shortcutted-if:
[[ $(echo -e "v0.1.5\nv0.1.4-2-p343h3d3" | sort -V | tail -1) != "v0.1.5" ]] && echo NEWER
Play around with the v0.1.4-2-p343h3d3
string. If you stick it up to 0.1.6
it'll echo out.
Now to bring this all back around to your command:
[[ $(echo -e "v0.1.5\n$(${HOME}/temp/.git describe --always --tags HEAD)" | sort -V | tail -1) != "v0.1.5" ]] && echo NEWER
Or you could break it down for readability:
THRESHOLD="v0.1.5"
VERSION=$(${HOME}/temp/.git describe --always --tags HEAD)
if [[ $(echo -e "$THRESHOLD\n$VERSION" | sort -V | tail -1) != "$THRESHOLD" ]]; then
echo GREATER
fi
0.1.5
? The0
seems relevant..."v1.1.0-3-p343h3d3"
OR"v2.0.7-4-p343h3d3"
OR"v10.0.9-5-p343h3d3"
? If you ignore the "leading" number (the digit 0 from"v0"
in your example), these will look like"1.0"
,"0.7"
, and"0.9"
, so these will all fail your test, even though you will probably want them all to pass your test of being">=1.5"
.