Linux Know-How provides a collection of introductory texts on often needed Linux skills. |
![]() |
Home ![]() ![]() ![]() |
|||
See also: The shift built-in | |||
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
|||
Making menus with the select built-inUse of selectThe select construct allows easy menu generation. The syntax is quite similar to that of the for loop: select WORD [in LIST]; do RESPECTIVE-COMMANDS; done LIST is expanded, generating a list of items. The expansion is printed to standard error; each item is preceded by a number. If in LIST is not present, the positional parameters are printed, as if in $@ would have been specified. LIST is only printed once. Upon printing all the items, the PS3 prompt is printed and one line from standard input is read. If this line consists of a number corresponding to one of the items, the value of WORD is set to the name of that item. If the line is empty, the items and the PS3 prompt are displayed again. If an EOF (End Of File) character is read, the loop exits. Since most users don't have a clue which key combination is used for the EOF sequence, it is more user-friendly to have a break command as one of the items. Any other value of the read line will set WORD to be a null string. The read line is saved in the REPLY variable. The RESPECTIVE-COMMANDS are executed after each selection until the number representing the break is read. This exits the loop. ExamplesThis is a very simple example, but as you can see, it is not very user-friendly:
Setting the PS3 prompt and adding a possibility to quit makes it better:
SubmenusAny statement within a select construct can be another select loop, enabling (a) submenu(s) within a menu. By default, the PS3 variable is not changed when entering a nested select loop. If you want a different prompt in the submenu, be sure to set it at the appropriate time(s).
|
|||
Home ![]() ![]() ![]() |