mkcmd.c (in net.sources) alternate approach

Don Steiny steiny at scc.UUCP
Sun Jul 8 10:44:06 AEST 1984


***

	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



More information about the Comp.unix mailing list