OT: case statements in sh

Jay R. Ashworth jra at baylink.com
Tue Jan 24 14:50:38 PST 2006


On Tue, Jan 24, 2006 at 07:33:42AM -0800, Jeff Harrison wrote:
> echo enter a number:
> read i
> case $i in
> 1)      echo "one";;
> 2)      echo "one"
>         echo "two";;
> 3)      echo "one"
>         echo "two"
>         echo "three";;
> *)      echo "$i";;
> esac

Though, as someone who's written a *lot* of shell code, I'd suggest you
do that so:

echo enter a number:
read i
case $i in
1)      echo "one"
	;;
2)      echo "one"
        echo "two"
	;;
3)      echo "one"
        echo "two"
        echo "three"
	;;
*)      echo "$i"
	;;
esac

It will make your life a lot easier, both in reading the case statment,
and in extending it.

(as a special case (no pun intended, really) that particular example is
actually Duff's Device, and could be implemented thus:

echo enter a number:
read i
case $i in
1)      echo "one"
2)	echo "two"
3)      echo "three"
	;;
*)      echo "$i"
	;;
esac

And would work the same way: cases fall-though in the shell just as
they do in C.)

Cheers,
-- jra
-- 
Jay R. Ashworth                                                jra at baylink.com
Designer                          Baylink                             RFC 2100
Ashworth & Associates        The Things I Think                        '87 e24
St Petersburg FL USA      http://baylink.pitas.com             +1 727 647 1274

	A: No.
	Q: Should I include quotations after my message body?


More information about the Filepro-list mailing list