Created
May 15, 2018 20:05
-
-
Save michaelgugino/774d073ebb95ed5c88313756cffc991e to your computer and use it in GitHub Desktop.
All bash version compare
This file contains 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 | |
xxx="go1.18.3" | |
xxx2="go1.10.2" | |
str_to_array() { | |
local IFS='.' | |
local NEW_ARRAY=(0 0 0 0) | |
local version_part_index=0 | |
for i in $1; do | |
NEW_ARRAY[$version_part_index]=$i | |
version_part_index=$((version_part_index+1)) | |
done | |
local IFS=' ' | |
echo "${NEW_ARRAY[*]}" | |
} | |
z1=$(str_to_array "$xxx") | |
z1=($z1) | |
z2=$(str_to_array "$xxx2") | |
z2=($z2) | |
echo "${z2[*]}" | |
version_part_index=0 | |
too_old=0 | |
for i in "${z2[@]}"; do | |
old_part="${z1[version_part_index]}"; | |
new_part="$i" | |
if (( new_part > old_part )); then | |
break | |
elif (( old_part > new_part )); then | |
echo "older" | |
too_old=1 | |
break | |
fi | |
version_part_index=$((version_part_index+1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment