steiny@scc.UUCP (Don Steiny) (07/08/84)
*** mkcmd.c, recently posted to net.sources has some examples of its use. This tells how to do the same thing on a single command line in either the Bourne shell or C shell. 1) To move all files ending with .c to .c.old. This can be done in the Bourne shell with the following command line: for i in *.c; do cp $i $i.old; done This line can be passed to the sh shell from C-shell: sh -c 'for i in *.c; do cp $i $i.old; done' 2) To move all files ending with ".ftn" to ".ft4": for i in *.ftn; do mv $i `basename $i ftn`ft4; done This can be passed to the Bourne shell from C-shell" sh -c 'for i in *.ftn; do mv $i `basename $i ftn`ft4; done' This is a good convention to master because the output of the statement body can be redirected, and this provides the one line solution to putting file names at the top of a file. Imagine you want to collect files and put the filenames at the top, say, for transmitting by cu. The following command line will take all the files in a directory, print ===filename=== and then cat the file. The output is saved in a file named "collected." for i in *; do echo ===$i===; cat $i; done > collected From C-shell: sh -c 'for i in *; do echo ===$i===; cat $i; done > collected' C-shell has a loop available from the terminal, but it is not as powerful as the Bourne shell. It is the "foreach". You must type multiple lines to use it and the output cannot be redirected. Here are the three examples (the question mark is a prompt from C-shell): # copy .c files to .c.old foreach i (*.c) ? cp $i $i.old ? end # rename *.ftn *.ft4 foreach i (*.ftn) ? mv $i `basename $i ftn`ft4 ? end Files can also be collected using redirects. foreach i (*) ? echo ===$i=== >> collected ? cat $i >> collected ? end Don Steiny Personetics 109 Torrey Pine Terr. Santa Cruz, Calif. 95060 (408) 425-0382 ucbvax!hplabs!pesnta!scc!steiny harpo!fortune!idsvax!scc!steiny
thomas@utah-gr.UUCP (Spencer W. Thomas) (07/10/84)
Actually, Don, your second example can be done much more easily in csh than you show (it's funny how we stick with the idioms we originally learned, even when they're not appropriate). > # rename *.ftn *.ft4 > > foreach i (*.ftn) > ? mv $i `basename $i ftn`ft4 > ? end The 'mv' line can be done more efficiently as ? mv $i $i:r.ft4 In fact, the csh provides a whole set of modifiers for collecting parts of file names $i:h "Head" - strips the last component off the path. $i:t "Tail" - returns the last component of the path. $i:r "Root" - strips the part after the last '.' off the filename (or path) $i:e "Extension" - just the part after the '.' (this may be new, I don't remember it before). Examples of use (suppose $i contains '/a/b/d.c') echo $i:h -> /a/b echo $i:t -> d.c echo $i:r -> /a/b/d echo $i:e -> c =Spencer
honey@down.FUN (07/13/84)
=== /***** down:net.unix / utah-gr!thomas / 10:41 am Jul 12, 1984*/ Actually, Don, your second example can be done much more easily in csh than you show (it's funny how we stick with the idioms we originally learned, even when they're not appropriate). we stick with the idioms we originally learned because they are the ones we can remember. it's funny how they work so well, even when they're not appropriate. peter