7.9. Menus: selectThe select command allows you to create simple menus, and has the syntax shown in Figure 7-17.
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:
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 $ _ |