[Novalug] command line option/file ordering

Jon LaBadie novalugml@jgcomp.com
Tue May 18 22:59:55 EDT 2010


On Tue, May 18, 2010 at 04:21:44PM -0400, Mike Miller wrote:
> On Tue, May 18, 2010 at 11:07 AM, Jon LaBadie <novalugml@jgcomp.com> wrote:
> > The recent discussion of grep caused me to experiment on
> > the command line and I came up with a surprise (at least
> > to this old UNIX-head) about command line ordering.
> > Maybe someone can tell me when and why linux and unix
> > diverged in this respect.  And how to tell if/when
> > command line ordering is important.
> 
> This is one of the many examples of GNU vs. POSIX, I like to make that
> distinction rather than Linux vs. UNIX.

Mea culpa.
I meant to leave linux out of this entirely,
note later I only use GNU.  This one slipped by.

> POSIX is a set of standards that GNU
> (libraries, utilities, etc) does attempt to conform to, but in some cases
> provides extensions which are the default on a GNU system.  Command-line
> shuffling is one of those extensions provided by the getopt(3) function in
> GNU's implementation of libc.  I believe all of the "standard" utilities use
> the same getopt so the behavior is consistent.
> 

I assumed they all used getopt(3), but I'd not coded anything with GNU's getopt
so was unaware of its extensions.

> > The SYNOPSIS section of a man page is supposed to document
> > the valid form(s) of the command line.  Some samples:
> >
> >  wc [OPTION]... [FILE]...
> >  ls [OPTION]... [FILE]...
> >  grep [options] PATTERN [FILE...]
> >  grep [options] [-e PATTERN | -f FILE] [FILE...]
> >
> > I learned to read this (particularly the first three)
> > as "options come before the filenames" (and the PATTERN
> > for grep).  A unix example:
> >
> >  $ ls .profile -l
> >  -l: No such file or directory
> >  .profile
> >  $
> >
> > But with the gnu utilities it seems options and files
> > can be in basically and order and mixed.
> 
> The magic word is POSIXLY_CORRECT.  If that environment variable is defined,
> it is supposed to be recognized by all GNU libraries, utilities, and so on, to
> disable the GNU extensions and more closely match the behavior dictated by
> POSIX.  This is documented sparsely here and there in various man and info
> pages.  For getopt, it does what you want.
>
Though I'd heard of POSIXLY_CORRECT, I was unaware of its usage in this regard.

> The POSIX rule says to stop when it hits the first argument that doesn't look
> like an option, or "--" by itself, which indicates "end of options".  The GNU
> behavior allows non-option arguments to essentially be bubble-sorted up
> through the argument list so all options are handled regardless of position
> and all non-options are left at the end, with their relative order to each
> other preserved.

GNU utilities do seem to honor the '--' as end of options even without setting
and exporting POSIXLY_CORRECT.

> > Also, I would have read the 4th SYNOPSIS example above
> > to mean that instead of PATTERN alone, I could use
> > "-e PATTERN" OR "-f FILE".  Note the OR denoted by the
> > "|".  In testing I find that should read AND/OR because
> > both the -e and the -f can be used on the same command
> > line and are additive.  Further, they can be used
> > multiple times mixed in with datafile names.
> 
> I agree, the additive property of these options is not explicitly stated in
> this man page.  Not the first or last piece missing from a man page.  When in
> doubt fall back to trial and error :)

Not the best way to write scripts using those options :)

jl
-- 
Jon H. LaBadie                  novalugml@jgcomp.com
 JG Computing
 12027 Creekbend Drive		(703) 787-0884
 Reston, VA  20194		(703) 787-0922 (fax)



More information about the Novalug mailing list