bug-coreutils
[Top][All Lists]
Advanced

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

Re: Possible bug in sort 5.97


From: Bob Proulx
Subject: Re: Possible bug in sort 5.97
Date: Fri, 28 Sep 2007 08:53:44 -0600
User-agent: Mutt/1.5.9i

Pádraig Brady wrote:
> address@hidden wrote:
> > I redirect the input to the same file as follows
> > $ sort testfile > testfile
> > the file suddenly gets empty.
> 
> cat warns you, but the file is still truncated.

Perhaps sort should include the same warning?  But by that time the
deed is already done.

> What's happening is the shell is truncating the file,
> before the command (sort in your case) reads it.

To be complete clear the order of operations is something like this
following:

 * the shell (e.g. /bin/bash) scans the line looking for shell
   meta-characters and notices the '>'.

 * the shell redirects output for the upcoming command into the
   specified output file, >testfile, truncating testfile to zero bytes
   so that it is ready to be written to by the soon to be invoked
   command

 * the shell invokes the command, in this case "sort", with a single
   file argument

 * sort reads testfile, sorts it, (it is empty at this point), writes
   the result to the standard output (still empty) and exits
   successfully

The sort command is invoked *after* the shell has redirected the
output to the file and truncated it.

As Andreas posted in his message the 'sort -o file file' option is
specifically designed to handle sorting files in place.  It will read
all of the input file completely before writing to the output file.
The coreutils manual documents it this way:

`-o OUTPUT-FILE'
`--output=OUTPUT-FILE'
     Write output to OUTPUT-FILE instead of standard output.
     Normally,
     `sort' reads all input before opening OUTPUT-FILE, so you can
     safely sort a file in place by using commands like `sort -o F F'
     and `cat F | sort -o F'.  However, `sort' with `--merge' (`-m')
     can open the output file before reading all input, so a command
     like `cat F | sort -m -o F - G' is not safe as `sort' might start
     writing `F' before `cat' is done reading it.

     On newer systems, `-o' cannot appear after an input file if
     `POSIXLY_CORRECT' is set, e.g., `sort F -o F'.  Portable scripts
     should specify `-o OUTPUT-FILE' before any input files.

Bob




reply via email to

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