Last active
February 29, 2020 16:57
-
-
Save davidalger/97b8cb888216864776fb19ff2180808a to your computer and use it in GitHub Desktop.
Unit Testing Version Comparison in Bash Script
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 | |
function version { | |
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; | |
} | |
# version operator version true/false (1/0) | |
UNITS=( | |
"(2.5.7 -le 2.5.6.9 0)" | |
"(2.4.10 -lt 2.4.9 0)" | |
"(2.4.8 -lt 2.4.10 1)" | |
"(2.5.6 -le 2.5.6 1)" | |
"(2.5.6 -lt 2.5.6 0)" | |
"(0.10.0.3 -gt 0.10.0 1)" | |
"(0.11.2 -gt 0.10.0 1)" | |
"(0.1.12 -gt 0.2.0 0)" | |
"(0.2.0 -ge 0.2.0 1)" | |
"(1.2.3 -ge 1.2.3.4 0)" | |
"('' -eq 0.0.0.0 1)" | |
) | |
# check version for each unit for pass/fail | |
for TEST in "${UNITS[@]}"; do | |
eval TEST=$TEST # Expand test as array | |
test $(version ${TEST[0]}) ${TEST[1]} $(version ${TEST[2]}) | |
[ $? -ne ${TEST[3]} ] && echo pass || echo fail | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment