C-Shell weirdness (count of words in a variable)

reschly at BRL.ARPA reschly at BRL.ARPA
Sun Mar 30 11:28:52 AEST 1986


>    I stand corrected.  It seems that $#var is either an error or a
> positive integer.  Counterintuitive is debatable, however; a variable
> with no words is kind of like saying something that is nothing.  The
> only exception I am aware of to this is that $#argv CAN be zero if it
> occurs in a shell which was given no arguments (which includes your
> login shell; try it).  THIS is what is counter-intutive to ME, that
> argv behaves differently than all other variables in this respect.

   Another case where the $#variable construct can be zero is in the
case of shell input.  Using the following code (from my .login):

	switch ($term)
		.
		.
		.
		case tty5620:
			echo -n "TERM type? (is: $term default: dmd):  "
			set termin=($<)
			if( $#termin ) then
				set term=$termin
			else
				set term=dmd
			endif
			breaksw
		default:
			echo -n "TERM type? (is: $term default: $term):  "
			set termin=($<)
			if( $#termin ) set term=$termin
	endsw

   In the above code, if only a carriage return is typed at the prompt,
the default (if different from the current value) is assigned to the
term variable. If anything else is typed, it is assigned to the term
variable.

   For those who have not seen this construct before,  be warned that
the format of the sets following the echos is critical.  I do not
remember exactly what the problem was, but I think it was that the
righthand side of the equals sign must be exactly as shown above.

				Later,
				    Bob



More information about the Comp.unix.wizards mailing list