OT: case statements in sh

Bob Stockler bob at trebor.iglou.com
Tue Jan 24 16:15:45 PST 2006


Jay R. Ashworth wrote (on Tue, Jan 24, 2006 at 05:50:38PM -0500):

| 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.)

And if you really get techincal about it, the final ";;"
is not required (thought it looks pretty there, and makes
things "look better".

Bob

-- 
Bob Stockler  +-+  bob at trebor.iglou.com  +-+  http://members.iglou.com/trebor
Author: MENU EDIT II - The BEST Creator/Editor/Manager for filePro User Menus.
Fully functional (time-limited) demos available by email request (specify OS).


More information about the Filepro-list mailing list