LiveFire Labs - Online UNIX Training - Company Logo


Online UNIX Training with Hands-on Internet Lab


"Eliminate the expense and inconvenience of classroom training without eliminating the classroom experience."

 


Home
Internet Lab
Console Access
Sample Course

Student Login


LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week - View


Questions? Call
1.888.843.1637 or send us email

May 5, 2003 (Tip) - Creating a Menu Using Select

LiveFire Labs' tip from last week explained what the PS2, PS3, and PS4 shell variables were used for.  You may recall that PS3 is used to re-define the prompt string displayed to users when the select command is ready to read a user's menu selection.  This week's tip takes a look at how a robust interactive menu is easily created with the select command.

A frequently-used syntax for the select command is:

select varname in opt1 opt2...optN
do
   case $varname in
      opt1)  command1;;
      opt2)  command2;;
      optN)  commandN;;
   esac
done

opt1-optN represents valid menu options, and command1-commandN represents the commands that will be executed when the corresponding menu option is selected by the user.

The following example is a simple menu that provides the user with system information:

PS3='Select an option and press Enter: '
select i in Date Host Users Quit
do
   case $i in
      Date)  date;;
      Host)  hostname;;
      Users)  who;;
      Quit)  break;;
   esac
done
When executed, this example will display the following:

1) Date
2) Host
3) Users
4) Quit
Select an option and press Enter:

If the user selects 1, the system date is displayed followed by the menu prompt:

1) Date
2) Host
3) Users
4) Quit
Select an option and press Enter: 1
Mon May 5 13:08:06 CDT 2003
Select an option and press Enter:

Pressing the Enter key without making a selection will re-display the menu and menu prompt:

Select an option and press Enter:
1) Date
2) Host
3) Users
4) Quit
Select an option and press Enter:

In the sample code above, notice that PS3 has been re-defined with a custom menu prompt.  If this line of code was omitted the default menu prompt (#?) would be displayed:  

1) Date
2) Host
3) Users
4) Quit
#?


Learn more...
  

If you are new to the UNIX or Linux operating system and would like to learn more about shells, shell commands, and shell variables, you may want to consider registering for LiveFire Labs' UNIX and Linux Operating System Fundamentals online training course.

If you already have a solid grasp of the fundamentals but would like to learn more about the Korn shell and basic and advanced shell scripting, taking our Korn Shell Scripting course will be beneficial to you.

Our innovative hands-on training model allows you to learn UNIX by completing hands-on exercises on real servers in our Internet Lab.


More Tips...

· 
Popular UNIX Tips from the Past

spacer Box Border
 

Receive the UNIX Tip, Trick, or Shell Script of the Week by Email


First Name:


Email Address:






   1.888.843.1637

Home - Contact us - Company info - Privacy Statement   

 
©2002-2003 LiveFire Labs.  All rights reserved.
Linux® is a registered trademark of Linus Torvalds, author and developer of this public domain operating system.
UNIX® is a registered trademark of The Open Group in the United States and other countries.