Subject: Re: "for" behaviour in /bin/sh
To: None <current-users@NetBSD.ORG>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: current-users
Date: 07/13/1998 12:30:51
For what it's worth, it seems to me that sh *should* take an empty list
for a "for" loop, for much the same reason that any code that does
something for each of a list of things should do nothing when passed a
zero-length list.

> So is the "correct" thing to do to replace

> 	for f in ${LIST}; do
> 	  echo $f
> 	done

> with

> 	for f in ${LIST} ""; do
> 	  if [ X"$f" != X"" ]; then
> 	    echo $f
> 	  fi
> 	done

Gag.  This may work, but only because ${LIST} can't produce zero-length
strings.  I think the right fix is to shrug at any shells that can't
handle it and say "fix your sh and it'll work fine".  If you can't do
that for some reason, go ahead and add a test, but I'd much rather keep
it out of the NetBSD sources; uglifying our code to make it portable to
other systems (a) runs against NetBSD tradition (just look at all the
err() and warn() calls instead of fprintf()) and (b) leads to
maintenance nightmares after kludges to support a half-dozen different
systems get grafted on.

If you really want to test, I'd much prefer to test *outside* the loop,
something akin to

case "${LIST}" in
	"")	;;
	*)	for f in ${LIST}; do
			echo $f
		done
		;;
esac

					der Mouse

			       mouse@rodents.montreal.qc.ca
		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B