[comp.unix.questions] Writing a loop in csh

tony@oha.UUCP (Tony Olekshy) (05/14/88)

A discussion of how to rename a set of files from the csh user prompt has
recently been carried out in this newsgroup.  This is really a specific case
of operating over each item in a group with a command.  Unfortunately, the csh
looping construct is messy and does not admit to the operation of the history
mechanism.  I use the csh as an interactive shell but use sh for scripts.
Since I am therefore familiar with the sh syntax, I use the following looping
mechanism from the csh prompt:

    %sh -cx 'for i in 1 2 3 4 5; do cp /dev/null $i.a; done'

    %sh -cx 'for file in ?.a; do mv $file `basename $file .a`.b; done'

If you know or can learn a little sh syntax, this works like a charm.  The -x
option gives you a running log of what is going on.  History-based editing
works unexpectedly well, because the whole looping command is a single
string, ie, !!:2 is

    'for file in ?.a; do mv $file `basename $file .a`.b; done'