LiveFire Labs: Online UNIX Training with Hands-on Internet Lab


"Taking a LiveFire Labs' course is an excellent way to learn Linux/Unix. The lessons are well thought out, the material is explained thoroughly, and you get to perform exercises on a real Linux/Unix box. It was money well spent."

Ray S.
Pembrook Pines, Florida



LiveFire Labs' UNIX and Linux Operating System Fundamentals course was very enjoyable. Although I regularly used UNIX systems for 16 years, I haven't done so since 2000. This course was a great refresher. The exercises were fun and helped me gain a real feel for working with UNIX/Linux OS. Thanks very much!"

Ming Sabourin
Senior Technical Writer
Nuance Communications, Inc.
Montréal, Canada

Read more student testimonials...




Receive UNIX Tips, Tricks, and Shell Scripts by Email







Custom Search



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

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 2012
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
#?