zsh-users
 help / color / mirror / code / Atom feed
* Version String Comparison
@ 2015-11-19 22:16 TJ Luoma
  2015-11-20  3:58 ` Daniel Shahaf
  2015-11-20  5:17 ` Matthew Martin
  0 siblings, 2 replies; 3+ messages in thread
From: TJ Luoma @ 2015-11-19 22:16 UTC (permalink / raw)
  To: Zsh-Users List


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"

I have been using a 'version' function below, which I found somewhere 
and I _thought_ worked for this scenario, but I'm now finding that it 
does not:

	INSTALLED_VERSION="6.7.0.36"
	LATEST_VERSION="6.7.0.0044"

	function version { echo "$@" | awk -F. '{ printf("28%03d%03d%03d\n", 
$1,$2,$3,$4); }'; }

	if [ $(version ${LATEST_VERSION}) -le $(version ${INSTALLED_VERSION}) ]
	then
			# No Update Needed
		echo "$NAME: Up To Date (Installed: $INSTALLED_VERSION and Latest: 
$LATEST_VERSION)"
		exit 0
	fi

But that gives me


	Up To Date (Installed: 6.7.0.36 and Latest: 6.7.0.0044)

(I'd also like to be able to compare version strings such as "5.0b5" and 
"5.0b10")

TjL


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Version String Comparison
  2015-11-19 22:16 Version String Comparison TJ Luoma
@ 2015-11-20  3:58 ` Daniel Shahaf
  2015-11-20  5:17 ` Matthew Martin
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Shahaf @ 2015-11-20  3:58 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh-Users List

TJ Luoma wrote on Thu, Nov 19, 2015 at 17:16:08 -0500:
> 
> 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"

If I didn't care about portability to other operating systems, I'd
probably just use the package manager's version comparator: 'dpkg
--compare-versions', 'pkg version --test-version', or local equivalent.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Version String Comparison
  2015-11-19 22:16 Version String Comparison TJ Luoma
  2015-11-20  3:58 ` Daniel Shahaf
@ 2015-11-20  5:17 ` Matthew Martin
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Martin @ 2015-11-20  5:17 UTC (permalink / raw)
  To: TJ Luoma; +Cc: zsh-users

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).


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-11-20  5:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-19 22:16 Version String Comparison TJ Luoma
2015-11-20  3:58 ` Daniel Shahaf
2015-11-20  5:17 ` Matthew Martin

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).