Tuesday, March 23, 2010

Number-based menus


You dont have to build your own "choose a number" function: ksh has one already! But note that it returns the value of the line, not the number of the line.
select word in one two three exit; do
 echo word is $word
 echo reply is $REPLY
 if [[ "$word" = "exit" ]] ; then
  break;
 fi
 done

This will print out a mini-menu like the following:
1) one
2) two
3) three
4) exit
#?
Note that this will loop between "do ... done" until you trigger a break somehow! (or until the user control-c's or whatever). So dont forget an exit condition!

No comments:

Post a Comment