Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1.1 9/4/83; site scc.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!cornell!uw-beaver!tektronix!hplabs!pesnta!scc!steiny From: steiny@scc.UUCP (Don Steiny) Newsgroups: net.unix Subject: mkcmd.c (in net.sources) alternate approach Message-ID: <237@scc.UUCP> Date: Sat, 7-Jul-84 20:44:06 EDT Article-I.D.: scc.237 Posted: Sat Jul 7 20:44:06 1984 Date-Received: Mon, 9-Jul-84 01:11:24 EDT Organization: Santa Cruz Computer, Aptos, Calif. Lines: 73 *** 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