[comp.unix.wizards] redirection before wildcards

kai@uicsrd.csrd.uiuc.edu (06/04/88)

It's pretty well known that commands like "grep whatever * > out" can
cause infinate loops, because C-shell will create the file "out"
before expanding the asterisk wildcard, and grep never reaches EOF
once it reaches the file "out".

Is there any reason for this behavior?  I don't understand why the
wildcard isn't expanded first.  Wouldn't this eliminate one of the
easier ways of filling up disk space?  Does anyone know if this could
cause compatibility problems?  It seems too often that people USE
bugs instead of FIXING them.

Do Bourne and Korn shells act this way as well?


Patrick Wolfe
Internet:  pwolfe@kailand.kai.com
UUCP:      ...!{uunet,ucbvax,ihnp4}!uiucuxc!kailand!pwolfe

shankar@hpclscu.HP.COM (Shankar Unni) (06/10/88)

>    It's pretty well known that commands like "grep whatever * > out" can
>    cause infinate loops, because C-shell will create the file "out"
>    before expanding the asterisk wildcard, and grep never reaches EOF
>    once it reaches the file "out".
> 
> Try `grep whatever * > .out'. Small consolation if you didn't :-)

But, for curiosity's sake, why exactly are redirections performed *before*
wildcard expansions?  For "historical" ( :-> ) reasons only? Or is there a
grander design behind it?

--
Shankar.

andrew@frip.gwd.tek.com (Andrew Klossner) (06/12/88)

[]

	"for curiosity's sake, why exactly are redirections performed
	*before* wildcard expansions?"

So that, in simple-minded shells without filename completion, I can type

	filter <in* | lpr

to mean

	filter <in_service_report.88Apr12.version2A | lpr

  -=- Andrew Klossner   (decvax!tektronix!tekecs!andrew)       [UUCP]
                        (andrew%tekecs.tek.com@relay.cs.net)   [ARPA]

andrew@alice.UUCP (06/14/88)

it is not that wildcards are expanded after redirection;
rather it is that bourne screwed the grammar. the grammar fo rthe shell
(such that it is) says essentially:
	redir word
rather than 
	redir list
and then check that list generates just one thing. (wildcard expansion
is only done for lists.)

Jeffrey_J_Vanepps@cup.portal.com (06/14/88)

Someone asked:
	"for curiosity's sake, why exactly are redirections performed
	*before* wildcard expansions?"

Andrew Klossner answered:

   So that, in simple-minded shells without filename completion, I can type

	filter <in* | lpr

   to mean

	filter <in_service_report.88Apr12.version2A | lpr

I reply:

Seems to me what you have described is the desired thing to happen, but
exactly opposite of what does happen and what the first writer was curious
about. You have expansion occurring before redirection.

I've always wondered why this was this way myself. Of course, when I wrote
a shell I did it my way :-)
--
	Jeffrey_J_Vanepps@cup.portal.com
	sun!portal!cup.portal.com!jeffrey_j_vanepps

No cute quote since Portal doesn't have a .signature facility.

gwyn@brl-smoke.ARPA (Doug Gwyn ) (06/15/88)

In article <7979@alice.UUCP> andrew@alice.UUCP writes:
>	redir word

Fortunately, automatic filename completion makes this much easier to
live with.  (Our shell will handle tilde expansion in "word", but
not variable substitution, which is the primary remaining gripe.)

rns@se-sd.sandiego.NCR.COM (Rick Schubert) (06/16/88)

In article <7979@alice.UUCP> andrew@alice.UUCP writes:
>
>
>it is not that wildcards are expanded after redirection;
>rather it is that bourne screwed the grammar. the grammar fo rthe shell
>(such that it is) says essentially:
>	redir word
>rather than 
>	redir list
>and then check that list generates just one thing. (wildcard expansion
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!!!^^^^^^
>is only done for lists.)
Maybe (notice that I qualified this -- I do not know for a fact) this is
the key.  Although wildcards are sometimes used for filename completion
(I assume this means "use the name of the FILE that matches this pattern"),
wildcards in general mean "use the NAMES of ALL the FILES that match this
pattern".  Since there can only be one filename per redirection, MAYBE
Steve Bourne (or John Mashey before him, if the Mashey shell exhibited
this behavior) reasoned that conceptually wildcard expansion didn't make
sense (even though it could sometimes work if the expansion matched exactly
one filename) for redirection and INTENTIONALLY wrote the grammar this way.

-- Rick Schubert (rns@se-sd.sandiego.NCR.COM)

egisin@watmath.waterloo.edu (Eric Gisin) (06/16/88)

In article <670021@hpclscu.HP.COM>, shankar@hpclscu.HP.COM (Shankar Unni) writes:
> But, for curiosity's sake, why exactly are redirections performed *before*
> wildcard expansions?  For "historical" ( :-> ) reasons only? Or is there a
> grander design behind it?

It is probably due to the way redirection is implemented.
If you use one algorithm for redirection, whether it be applied to
executable commands, built-ins, or control statements, then
you perform redirection before you have determined the type of command.
Argument and assignment expansion occurs after you have determined
you have a simple-command.

I haven't checked whether this is true for the SysV shell,
but it is the way I implemented things.