C-Shell weirdness

jsdy at hadron.UUCP jsdy at hadron.UUCP
Sat Mar 29 18:21:00 AEST 1986


In article <676 at nbires.UUCP> nose at nbires.UUCP (Steve Dunn) writes:
>           if ($arg == '-b') echo 'UNIX is fun'
>Will get the error "If: missing filename" if $arg happens to equal
>-x or -e or any of the other constructs used to test file attributes.
>	 if (x$arg == 'x-b') echo 'UNIX is fun'
>Is there a better way to do this and why does this happen in the first
>place?

[The rest of this was ably answered elsewhere.  HOWEVER ...]

Consider what the command line is when $arg is -e or -x or whatever:
	if (-e == '-b') ...
Now, doesn't that just look like it's asking to evaluate -e on file
"==", or something like that?  Of course the parser recognises "=="
as a relation, and so complains instead that there's no file name.

Your alternative is actually a venerable shell-script idiom to cancel
the flag effect of the "-".  However, you really should put $arg in
double-quotes to negate the effect of an in-valid string:
	set arg = "-b -e"
	if (x$arg == '-b')
becomes
	if (x-b -e == '-b')
with who knows what syntax error reports.  I'd use:
	if ("X$arg" == "X-b") ...
-- 

	Joe Yao		hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.unix.wizards mailing list