Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Version String Comparison



On Thu, Nov 19, 2015 at 05:16:08PM -0500, TJ Luoma wrote:
> 
> I am looking for a zsh way to compare version numbers, which is smart enough
> to know that, for example
> 
> "6.7.0.36" is less than "6.7.0.0044"

vercomp() {
	[ "$1" = "$2" ] && return
	[ "${1%%.*}" -gt "${2%%.*}" ] && return 1
	[ "${1%%.*}" -lt "${2%%.*}" ] && return 2
	vercomp "${1#*.}" "${2#*.}"
}

% vercomp 3 4; echo $?              
2

% vercomp 1 1; echo $?
0

% vercomp 6.7.0.36 6.7.0.0044; echo $?
2

Salt to taste for return values and beta handling (and note no error
handling is done).



Messages sorted by: Reverse Date, Date, Thread, Author