csh problem involving nested ifs?

Marty Olevitch marty at wuphys.UUCP
Tue Jul 11 02:02:05 AEST 1989


It appears that csh does not correctly handle nested if-then-else
statements. The following script illustrates the problem, which appears
in versions of csh on MORE/bsd (mt Xinu 4.3), SunOS 3.1, SunOS 4.0, and
Ultrix 2.0. Am I missing some bug in this script? If not, it looks like
a bug in csh (an equivalent sh program works fine).

#! /bin/csh
set v1 = $1
set v2 = $2
echo "v1 = $v1 and v2 = $v2"
if($v1 == 1) then
	echo checkpoint 1
	if($v2 == 1) then
		echo 11
	else 
		echo 12
	endif
	echo checkpoint 2
else 
	echo checkpoint 3
	if($v2 == 1) then
		echo 21
	else 
		echo 22
	endif
	echo checkpoint 4
endif

Here is a little chart showing the command lines issued, the expected
results, and the actual results.

command line	expect			get
------------	------			-------
iftest 1 1	checkpoint 1		checkpoint 1
		11			11
		checkpoint 2		checkpoint 2
					checkpoint 4
					
iftest 1 2	checkpoint 1		checkpoint 1
		12			12
		checkpoint 2		checkpoint 2
					checkpoint 4
						
iftest 2 1	checkpoint 3		12
		21			checkpoint 2
		checkpoint 4		checkpoint 4

iftest 2 2	checkpoint 3		12
		22			checkpoint 2
		checkpoint 4		checkpoint 4

Marty Olevitch				Internet: marty at wuphys.wustl.edu
Washington University			UUCP:     uunet!wugate!wuphys!marty
Physics Department, Campus Box 1105	Bitnet:   marty at wuphys.WUSTL
St Louis MO 63130 USA			Tel:      (314) 889-6285



More information about the Comp.unix.wizards mailing list