DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Automating frequent tasks

Generating a simple menu: the select statement

Although not used in the readability analysis sample program, the select statement can be used to simply generate menus. It is restricted to the Korn shell, and has no equivalent in the Bourne and C shells. It has the following syntax:

select name [in list]
do
statements # statements use $name
done

The in list construct can be omitted, in which case, list defaults to $@ (see ``Passing arguments to a shell script'').

The select statement generates a menu from the entries in list, one per line, with each preceded by a number. It also displays a prompt, by default a hash sign followed by a question mark (#?). The user's response to the prompt is stored in the variable name; on the basis of the value of $name, the appropriate statement is executed. select then prompts for another choice, unless an explicit break command causes the loop to terminate.

The following trivial sample code illustrates select in use:

   print "Choose a dinosaur:"
   select dino in allosaurus tyrannosaurus brontosaurus triceratops
   do
   case $dino in
   allosaurus ) print "Jurassic carnosaur" ;;
   tyrannosaurus ) print "Cretaceous carnosaur" ;;
   brontosaurus ) print "Jurassic herbivore" ;;
   triceratops ) print "Cretaceous carnosaur" ;;
   *) print "invalid choice" ;;
   esac
   break
   done
The following shows the code in use (the program is called dino_db):
   Choose a dinosaur:
   1) allosaurus
   2) tyrannosaurus
   3) brontosaurus
   4) triceratops
   #?

Next topic: Expanding the example: counting words
Previous topic: Making multiway choices: the case statement

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003