[comp.unix.questions] 'foreach' help needed

larry@sgistl.SGI.COM (Larry Autry) (07/02/88)

I would like some advice on what the syntax should be for 'foreach'. I
already know it's use but I haven't seen any examples in documentation.
-- 
					Larry Autry
larry@sgistl.sgi.com
       or
{ucbvax,sun,ames,pyramid,decwrl}!sgi!sgistl!larry

wnp@dcs.UUCP (Wolf N. Paul) (07/02/88)

In article <5639@sgistl.SGI.COM> larry@sgistl.SGI.COM (Larry Autry) writes:
>I would like some advice on what the syntax should be for 'foreach'. I
>already know it's use but I haven't seen any examples in documentation.

I assume you mean the CSH construct equivalent to SH/KSH "for var in list do".

Here is the syntax:

foreach varname ( list )
	command $varname
	...
end

Here is an example:

foreach file ( *.c )
	mv $file $file.orig
end

Main differences from sh/ksh:  parentheses around list, no "do/done", 
terminate with "end".

Bugs:  I've worked with versions of csh where the parentheses needed to 
be surrounded by whitespace -- i.e. it has to be " ( *.c ) " rather than
"(*.c)".
-- 
Wolf N. Paul * 3387 Sam Rayburn Run * Carrollton TX 75007 * (214) 306-9101
UUCP:     killer!dcs!wnp                 ESL: 62832882
DOMAIN:   wnp@dcs.UUCP                   TLX: 910-380-0585 EES PLANO UD

dberg@cod.NOSC.MIL (David I. Berg) (07/05/88)

In article <5639@sgistl.SGI.COM>, larry@sgistl.SGI.COM (Larry Autry) writes:
> I would like some advice on what the syntax should be for 'foreach'. I
> already know it's use but I haven't seen any examples in documentation.
> -- 

foreach i (list)
	
	echo $i
	more script
	   |
	   |
	   |
	   V

end

where i is any variable name
  and list is a list of values to be given to i for each iteration
      through the foreach loop.  A continue statement in the loop 
      causes an unconditional branch to the end statment (to start
      the next iteration of the loop); a break statement in the loop
      causes an unconditional branch to the first statement following
      end (terminating the loop).  List can be constructed by executing
      a csh command, such as `cat file` or `ls xxxxx` where file contains 
      the list of variables to be given to i, or xxxxx is a directory name
      or a list of filenames.

The possibilities are endless.


-- 
David I. Berg (dberg@nosc.mil)
GENISYS Information Systems, Inc., 4250 Pacific Hwy #118, San Diego, CA 92110
MILNET: dberg@nosc.mil
UUCP:   {ihnp4 akgua decvax dcdwest ucbvax}!sdcsvax!noscvax!dberg