Section 6.12. Menus: select


[Page 236 (continued)]

6.12. Menus: select

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

Figure 6-41. Description of the select shell command.


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


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.


  • [Page 237]
  • 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 the list are executed, and then the user is prompted for another choice.

The next example is a recoding of the menu-selection example from earlier in the chapter. It replaces the while loop and termination logic with a simpler select command.

$ cat newmenu.sh        ...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 $ sh newmenu.sh            ...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