Bourne Shell bug? Have a look..

Martin Weitzel martin at mwtech.UUCP
Mon Feb 4 03:07:51 AEST 1991


In article <1067 at mwtech.UUCP> martin at mwtech.UUCP I wrote:
>
>You further know (or again you just learn it), that there can be more
>than one redirection per line:
>
>	echo hello >file foo >baz
>
>Now: Which redirection is the one that is finally in effect when the
>programm (the `echo' in the above example) runs, if you combine both
>of the above "strange" ways to do I/O-direction? (What I want is not
>the answer to a specific example, but the general rule!) I'll post the
>answer in a week or so if noone solves this).

Thanks to "uunet!bria!mike (Michael Stefanik)" and "chet at odin.INS.CWRU.Edu
(Chet Ramey)" who both cared to write a detailed followup to my question.
What I asked for was the "general rule" implemented in the Bourne-Shell
and what I found was (at least Shell versions I know):

	1) Scan the command line for the last "true" arg (i.e. the
	   last arg which is not a redirection).
	2) Make one trip round the command line processing the
	   I/O redirection, starting from the last arg (see above)
	   and jumping to the beginning of the line if the end is
	   reached.
	3) For all the ">" encountered open this file (creating it
	   or truncating its size to zero). The last redirection is
	   the one that stays in effect.

Examples:
		echo hello >file foo >baz
		                 ^^^------+
		+----<-----<-----<--------+
		+--------------->

		echo hello foo >baz >file
		           ^^^------------+
		+----<-----<-----<--------+
		+--------->

		>file echo hello foo >baz
		                 ^^^ -----+
		+----<-----<-----<--------+
		+--------------->

In any of the three cases the result is an empty file "baz" and the
string "hello foo" in "file".

Note that I neither claim that what the shell does here is useful or
sensible, that it is a bug or a feature, or that some Shell script
should exploit or otherwise depend on this behaviour. The point I
wanted to make was simply that the rule demonstrated above is a very
simple one, though on first glance it seemed that several exceptions
from a general rule were necessary to describe the behaviour.
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.unix.shell mailing list