Cshell question: taking wordlists as a single string

Rob McMahon cudcv at warwick.ac.uk
Sun Aug 19 00:11:41 AEST 1990


In article <OL95_B2 at xds13.ferranti.com> peter at ficc.ferranti.com (Peter da Silva) writes:
>In article <3251 at syma.sussex.ac.uk>, andy at syma.sussex.ac.uk (Andy Clews) writes:
>>       foreach i ($*)
>
>> Basically, then, can Cshell cope with word-lists as single arguments, or
>> must I write a C program to do the job (or try sh or ksh?)
>
>Try sh ... By and large, csh is a poor language for writing programs in. Sh
>is much better.

In fact, IMHO, handling wordlists is one area where csh beats sh hands down.
(I agree that sh is normally better for writing scripts.)  The answer is
simply

	foreach i ( $*:q )

It's very much easier to sort arguments out in csh than sh, in sh people tend
to do things like

	while [ $# -gt 0 ]; do case "$i" in
		-a) shift; aprogargs="$aprogargs $1"; shift;;
		-b) shift; bprogargs="$bprogargs $1"; shift;;
		...
	esac; done
	aprog $aprogargs | bprog $bprogargs

which of course breaks horribly if any arguments have spaces in them.  Doing
it right involves all sorts of horrible kludges with eval's and trying to get
correctly quoted single quotes in the string, while avoiding trying to quote
the single quotes that should have been there with the right number of \'s.

In csh it's just

	while ( $#argv > 0 )
		switch ( $1:q )
			case "-a":
				shift
				aprogargs = ( $aprogargs:q $1:q )
				shift
				breaksw
			case "-b":
				shift
				bprogargs = ( $bprogargs:q $1:q )
				shift
				breaksw
			...
		endsw
	end
	aprog $aprogargs:q | bprog $bprogargs:q

and there is no problem with funny arguments at all.  When I'm doing this sort
of thing I'm often tempted into using csh, in spite of its parsing problems
and less flexible traps and redirection.

I know ksh has `set -A array', but does it have an equivalent of "$@" for
arrays other than the positional paramters ?  We don't have ksh, and can't
afford to get it, but when bash gets a bit more solid I will switch to it if
it has an equivalent of csh's $array:q.

Rob
--
UUCP:   ...!mcsun!ukc!warwick!cudcv	PHONE:  +44 203 523037
JANET:  cudcv at uk.ac.warwick             INET:   cudcv at warwick.ac.uk
Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England



More information about the Comp.unix.questions mailing list