Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.unix.shell Subject: Re: Bourne Shell bug? Have a look.. Message-ID: <1084@mwtech.UUCP> Date: 3 Feb 91 16:07:51 GMT References: <1991Jan16.152933.7288@usenet.ins.cwru.edu> <1991Jan16.153557.15548@ms.uky.edu> <1067@mwtech.UUCP> <1991Jan31.161817.16865@usen Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 55 In article <1067@mwtech.UUCP> martin@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@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@mwtech.UUCP, voice: 49-(0)6151-6 56 83