Section 7.9. Menus: select


[Page 267 (continued)]

7.9. Menus: select

The select command allows you to create simple menus, and has the syntax shown in Figure 7-17.

Figure 7-17. Description of the select shell command.


select name [ in {word }+ ]
do
  list
done



[Page 268]

The select command displays a numbered list of the words specified by the in clause to the standard error channel, displays the prompt stored in the special variable PS3, and then waits for a line of user input. When the user enters a line, it's stored in the predefined variable REPLY, and then one of three things occurs:

  • If the user entered one of the listed numbers, name is set to that number, the commands in list are executed, and then the user is prompted for another choice.

  • If the user entered a blank line, the selection is displayed again.

  • If the user entered an illegal choice, name is set to null, the commands in list are executed, and then the user is prompted for another choice.

The next example is a recoding of the menu selection example from Chapter 6, "The Bourne Again Shell." It replaces the while loop and termination logic with a simpler select command.

$ cat menu.ksh          ...list the script. echo menu test program select reply in "date" "pwd" "pwd" "exit" do  case $reply in    "date")      date      ;;    "pwd")      pwd      ;;    "exit")      break      ;;    *)      echo illegal choice      ;;  esac done $ ksh menu.ksh              ...execute the script. menu test program 1) date 2) pwd 3) pwd 4) exit #? 1 Fri May  6 21:49:33 CST 2005 #? 5 illegal choice #? 4 $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net