Posts Tagged version
Shell Script Version Compare (vercmp)
Posted by A R Baboon in Uncategorized on April 5, 2010
Here is something useful. A very short shell script to compare standard version numbers of the form ##.##.## with up to four segments. It returns the difference of the first differing segment just like strcmp.
#!/bin/sh
expr '(' "$1" : '\([^.]*\)' ')' '-' '(' "$2" : '\([^.]*\)' ')' '|' \
'(' "$1.0" : '[^.]*[.]\([^.]*\)' ')' '-' '(' "$2.0" : '[^.]*[.]\([^.]*\)' ')' '|' \
'(' "$1.0.0" : '[^.]*[.][^.]*[.]\([^.]*\)' ')' '-' '(' "$2.0.0" : '[^.]*[.][^.]*[.]\([^.]*\)' ')' '|' \
'(' "$1.0.0.0" : '[^.]*[.][^.]*[.][^.]*[.]\([^.]*\)' ')' '-' '(' "$2.0.0.0" : '[^.]*[.][^.]*[.][^.]*[.]\([^.]*\)' ')'Update: Added zeros to deal with unbalanced version compares