When is an expression "true"?

Jonathan I. Kamens jik at athena.mit.edu
Thu May 9 11:03:20 AEST 1991


  The csh man page does not document the ability to have if statements without
parentheses around the boolean expression, and it is *not* something you
should rely on, because it is not supported and will break much of the time.

  In the particular example you gave:

	set f=`ls`
	if $#f > 1 then
	   ...
	endif

breaks because the shell notices the '>' and assumes that it is redirection
rather than a numerical comparison.  I suspect there's a file called "1" in
the directory in which you ran this shell script.

  I should probably mention that parentheses aren't the only thing you can use
-- you can also use curly braces, which cause a csh if statment to function
much the same way the bourne shell's if statement functions.  This:

	foo
	if ($status == 0) then
		...
	endif

is equivalent to this:

	if { foo } then
		...
	endif

This is a useful feature whose documentation is unfortunately buried deep in
the man page for csh, and is only mentioned in passing -- "Also available in
expressions as primitive operands are command executions enclosed in `{' and
`}'..."  Of course, it never explains what "command executions" are.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.shell mailing list