Last active
April 19, 2021 18:22
-
-
Save soliton-/f1f2d08cee3718d9da88107aac4cf171 to your computer and use it in GitHub Desktop.
version compare for bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# compare version $1 and $2; returns 0 if version $1 >= $2 | |
# versions must be numeric only | |
vcompare() { | |
local v1=$1 v2=$2 v1a v2a n1 n2 max i | |
IFS=. read -ra v1a <<< "$v1"; IFS=. read -ra v2a <<< "$v2" | |
((n1=${#v1a[@]}, n2=${#v2a[@]}, max=(n1 > n2 ? n1 : n2))) | |
for ((i=0; i<max; ++i)) | |
do | |
((v1a[i] == v2a[i])) && continue | |
((v1a[i] > v2a[i])) | |
return | |
done | |
} |
kurahaupo
commented
Apr 19, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment