bug-coreutils
[Top][All Lists]
Advanced

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

Re: patch: contrib/compare_tests


From: Paul Eggert
Subject: Re: patch: contrib/compare_tests
Date: Wed, 03 May 2006 16:24:43 -0700
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Zack Weinberg <address@hidden> writes:

>> > >      On older systems, `sort' supports an obsolete origin-zero syntax
>> > >   `+POS1 [-POS2]' for specifying sort keys.  This obsolete behavior can
>> > >   be enabled or disabled with the `_POSIX2_VERSION' environment variable
>> > >   (*note Standards conformance::), but portable scripts should avoid
>> > >   commands whose behavior depends on this variable.  For example, use
>> > >   `sort ./+2' or `sort -k 3' rather than the ambiguous `sort +2'.
>
> bug-coreutils: this advice is wrong.  Portable scripts *must* use
> 'sort +2' if they want the effect of 'sort -k 3',

But that suggestion is incorrect as well, since it's not portable
to my host:

  $ sort +2
  sort: open failed: +2: No such file or directory

However, your underlying point is reasonable.  The advice you quoted
from the manual is fine for those who are worried only about porting
to standard hosts, but it is misleading for those who are also worried
about porting to hosts that support only the obsolete syntax.

I installed this patch into the coreutils documentation to fix the
problem.  Thanks for reporting it.

2006-05-03  Paul Eggert  <address@hidden>

        * coreutils.texi (head invocation, tail invocation, sort invocation):
        Give advice about porting to hosts that support only obsolete syntax.
        Problem reported by Zack Weinberg.

--- coreutils.texi      23 Apr 2006 21:46:54 -0000      1.326
+++ coreutils.texi      3 May 2006 23:10:04 -0000       1.327
@@ -2435,8 +2435,11 @@ For compatibility @command{head} also su
 specified first.  @var{count} is a decimal number optionally followed
 by a size letter (@samp{b}, @samp{k}, @samp{m}) as in @option{-c}, or
 @samp{l} to mean count by lines, or other option letters (@samp{cqv}).
-New scripts should use @option{-c @var{count}} or @option{-n
address@hidden instead.
+Scripts intended for standard hosts should use @option{-c @var{count}}
+or @option{-n @var{count}} instead.  If your script must also run on
+hosts that support only the obsolete syntax, it is usually simpler to
+avoid @command{head}, e.g., by using @samp{sed 5q} instead of
address@hidden -5}.
 
 @exitstatus
 
@@ -2620,8 +2623,6 @@ file.  In the option, @var{count} is an 
 followed by a size letter (@samp{b}, @samp{c}, @samp{l}) to mean count
 by 512-byte blocks, bytes, or lines, optionally followed by @samp{f}
 which has the same meaning as @option{-f}.
-New scripts should use @option{-c @var{count}[b]},
address@hidden @var{count}}, and/or @option{-f} instead.
 
 @vindex _POSIX2_VERSION
 On older systems, the leading @samp{-} can be replaced by @samp{+} in
@@ -2629,12 +2630,24 @@ the obsolete option syntax with the same
 obsolete usage overrides normal usage when the two conflict.
 This obsolete behavior can be enabled or disabled with the
 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
-conformance}), but portable scripts should avoid commands whose
-behavior depends on this variable.
-For example, use @samp{tail -- - main.c} or @samp{tail main.c} rather than
-the ambiguous @samp{tail - main.c}, @samp{tail -c4} or @samp{tail -c 10
-4} rather than the ambiguous @samp{tail -c 4}, and @samp{tail ./+4}
-or @samp{tail -n +4} rather than the ambiguous @samp{tail +4}.
+conformance}).
+
+Scripts intended for use on standard hosts should avoid obsolete
+syntax and should use @option{-c @var{count}[b]}, @option{-n
address@hidden, and/or @option{-f} instead.  If your script must also
+run on hosts that support only the obsolete syntax, you can often
+rewrite it to avoid problematic usages, e.g., by using @samp{sed -n
+'$p'} rather than @samp{tail -1}.  If that's not possible, the script
+can use a test like @samp{if tail -c +1 </dev/null >/dev/null 2>&1;
+then @dots{}} to decide which syntax to use.
+
+Even if your script assumes the standard behavior, you should still
+beware usages whose behaviors differ depending on the @acronym{POSIX}
+version.  For example, avoid @samp{tail - main.c}, since it might be
+interpreted as either @samp{tail main.c} or as @samp{tail -- -
+main.c}; avoid @samp{tail -c 4}, since it might mean either @samp{tail
+-c4} or @samp{tail -c 10 4}; and avoid @samp{tail +4}, since it might
+mean either @samp{tail ./+4} or @samp{tail -n +4}.
 
 @exitstatus
 
@@ -3662,10 +3675,15 @@ On older systems, @command{sort} support
 syntax @address@hidden address@hidden for specifying sort keys.
 This obsolete behavior can be enabled or disabled with the
 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
-conformance}), but portable scripts should avoid commands whose
-behavior depends on this variable.
-For example, use @samp{sort ./+2} or @samp{sort -k 3} rather than
-the ambiguous @samp{sort +2}.
+conformance}).
+
+Scripts intended for use on standard hosts should avoid obsolete
+syntax and should use @option{-k} instead.  For example, avoid
address@hidden +2}, since it might be interpreted as either @samp{sort
+./+2} or @samp{sort -k 3}.  If your script must also run on hosts that
+support only the obsolete syntax, it can use a test like @samp{if sort
+-k 1 </dev/null >/dev/null 2>&1; then @dots{}} to decide which syntax
+to use.
 
 Here are some examples to illustrate various combinations of options.
 




reply via email to

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