[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Yet Another test option

From: Eric Blake
Subject: Re: Yet Another test option
Date: Wed, 06 Jul 2011 11:19:23 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20110428 Fedora/3.1.10-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.10

On 07/06/2011 10:37 AM, Bruce Korb wrote:
> On 07/06/11 09:03, Chet Ramey wrote:
>>> /usr/bin/test ?
>>> Do this first in the binary then migrate to bash's test?
>> I was actually making an argument for an entirely separate utility to do
>> this.  That could be a shell script encapsulating the proper version
>> comparison logic.
> which basically means a script wrapping "sort -V" and testing whether
> the arguments got reordered or not:
>   if test "X$1" = "X$3"
>   then is_eq=true ; is_lt=false
>   else
>     is_eq=false
>     first=$(printf '%s\n%s\n' "$1" "$2" | sort -V | head -1)
>     test "X$first" = "X$1" && is_lt=true || is_lt=false
>   fi

Oh, that's rather heavyweight - a command substitution and 3 pipeline
components.  Why not just one child process, by using sort -c and a heredoc?

is_eq=false is_lt=false
if test "x$1" = "$x2"; then
elif sort -cV <<EOF 2>/dev/null; then

> and if that proved insufficient, then "sort -V" would need an adjustment.
> I would not expect "sort -V" and a version test to disagree.

The code that coreutils uses for 'sort -V' is part of gnulib - the
filevercmp module.  That file (filevercmp.c) is pretty stable nowadays,
with the last algorithmic change being in April 2009 and no recent
complaints about unexpected behavior (whereas glibc's strverscmp is
locked into behavior, but that behavior raises complaints).  For
reference, the documentation is:

/* Compare version strings:

   This function compares strings S1 and S2:
   1) By PREFIX in the same way as strcmp.
   2) Then by VERSION (most similarly to version compare of Debian's dpkg).
      Leading zeros in version numbers are ignored.
   3) If both (PREFIX and  VERSION) are equal, strcmp function is used for
      comparison. So this function can return 0 if (and only if) strings S1
      and S2 are identical.

   It returns number >0 for S1 > S2, 0 for S1 == S2 and number <0 for S1
< S2.

   This function compares strings, in a way that if VER1 and VER2 are
   numbers and PREFIX and SUFFIX (SUFFIX defined as
   are strings then VER1 < VER2 implies filevercmp (PREFIX VER1 SUFFIX,

   This function is intended to be a replacement for strverscmp. */

However, I don't see any reason to add extensions to coreutils' test
unless we have some agreement that we plan to add the same extension to
other places like the bash builtin test at the same time.  Since we've
already demonstrated that version comparisons are a pretty trivial
wrapper around sort, I'm not seeing much justification in favor of
bloating test to make version testing builtin.

Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]