MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
May
Sun Mon Tue Wed Thu Fri Sat
9
       
2016
Months
May


Mon, May 09, 2016 11:25 pm

Creating a Bash menu

You can create a menu of options for someone to choose from in a Bash script using the select construct. E.g., the following Bash script will present a text-based menu with three choices: "Option 1", "Option 2", and "Option 3".

#!/bin/bash
# Bash Menu Script Example
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select option in "${options[@]}"
do
case $option in
"Option 1")
echo "You chose option 1"
;;
"Option 2")
echo "You chose option 2"
;;
"Option 3")
echo "You chose option 3"
;;
"Quit")
echo "Quitting the program"
break
;;
*) echo invalid option;;
esac
done

Note: to make a script executable from your account, you must set the file permissions for the script to grant execute permission for your account, e.g. with chmod u+x menu, if the file name for the script was named menu.

When the script is run, it will display the following text:

1) Option 1
2) Option 2
3) Option 3
4) Quit
Please enter your choice:

If the person running the script types "1", the script will display "You chose option 1". It will display "You chose option 2", if he/she types "2", "You chose option 3", if he/she types "3" and, if he/she types "4", will display "Quitting the program" and then exit from the script. Hitting any other key will cause the script to display "invalid option" while allowing the user to type another key for one of the other options.

By setting the PS3 variable you can control the prompt that is displayed to the user. If it wasn't set, the user would see a default prompt, which is #?, displayed, instead, as shown below:

1) Option 1
2) Option 2
3) Option 3
4) Quit
#?

The select construct has the following format:

select Word in Array
do
  commands-for-options
done

Word and Array are names of your choosing. If you are unfamiliar with what an array represents, think of it as a collection of items. You can think of it as a list, though in computer programming languages those are not necessarily synonymous. You can find more information at Bash Arrays.

In the example above, I chose "Option" for Word and "Options" for the array name. The array, i.e., the list of options, was created with the following command:

options=("Option 1" "Option 2" "Option 3" "Quit")

The first element of the array, which contains 4 elements in this case, is "Option 1", the next "Option 2", etc.

The ${options[@] in select option in "${options[@]}" returns each item in the array as a separate word.

Between the case $option in and esac (case reversed), I can insert the commands to be carried out for each option that is selected. Including a *) allows the script to take some action when any key not associated with a valid option is typed. Each clause in the case statement must be terminated with ";;". Each case statement is ended with the esac statement. For further information on the case statement, see Using case statements in Machtelt Garrels Bash Guide for Beginners.

Bash is a common shell on Unix and Linux systems and is the default shell when you open the Terminal application on a Mac OS X system. And even Microsoft has announced that it will provide a Bash shell in the Windows 10 Anniversary Update, which is expected to be shipped this summer. So you can create text-based menus using this method on a variety of operating systems.

[/os/unix/bash] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo