Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!usc!apple!snorkelwacker!bloom-beacon!eru!luth!sunic!mcsun!ukc!warwick!cudcv From: cudcv@warwick.ac.uk (Rob McMahon) Newsgroups: comp.unix.questions Subject: Re: Cshell question: taking wordlists as a single string Message-ID: <1990Aug18.141141.24890@warwick.ac.uk> Date: 18 Aug 90 14:11:41 GMT References: <3251@syma.sussex.ac.uk> Sender: news@warwick.ac.uk (Network news) Organization: Computing Services, Warwick University, UK Lines: 64 In article peter@ficc.ferranti.com (Peter da Silva) writes: >In article <3251@syma.sussex.ac.uk>, andy@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@uk.ac.warwick INET: cudcv@warwick.ac.uk Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England