Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm creating a simple bash script and I want to create a select menu in it, like this: ./script echo "Choose your option:"

1) Option 1
2) Option 2
3) Option 3
4) Quit

And according to user's choice, I want different actions to be executed. I'm a bash shell scripting noob, I've searched the web for some answers, but got nothing really concrete.

So, shell script masters, how should I do this? Thanks.

share|improve this question
2  
I'm pretty sure this would fit better on Stack Overflow. But just as a heads-up, bash has a select statement that will do this. See for example stackoverflow.com/questions/3200252/… –  David Z Aug 8 '10 at 22:46
    
Another good place for this question would be unix.stackexchange.com –  MountainX Jun 30 '13 at 8:46
add comment

closed as too localized by Luis Alvarado Mar 14 '13 at 16:35

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

9 Answers

#!/bin/bash
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "you chose choice 1"
            ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done
share|improve this answer
add comment

Not a new answer per se, but since there's no accepted answer yet, here are a few coding tips and tricks, for both select and zenity:

title="Select example"
prompt="Pick an option:"
options=("A" "B" "C")

echo "$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do 

    case "$REPLY" in

    1 ) echo "You picked $opt which is option $REPLY";;
    2 ) echo "You picked $opt which is option $REPLY";;
    3 ) echo "You picked $opt which is option $REPLY";;

    $(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
    *) echo "Invalid option. Try another one.";continue;;

    esac

done

while opt=$(zenity --title="$title" --text="$prompt" --list \
                   --column="Options" "${options[@]}"); do

    case "$opt" in
    "${options[0]}" ) zenity --info --text="You picked $opt, option 1";;
    "${options[1]}" ) zenity --info --text="You picked $opt, option 2";;
    "${options[2]}" ) zenity --info --text="You picked $opt, option 3";;
    *) zenity --error --text="Invalid option. Try another one.";;
    esac

done

Worth mentioning:

  • Both will loop until the user explicitly chooses Quit (or Cancel for zenity). This is a good approach for interactive script menus: after a choice is selected and action performed, menu is presented again for another choice. If choice is meant to be one-time only, just use break after esac (the zenity approach could be further reduced also)

  • Both case are index-based, rather than value-based. I think this is easier to code and maintain

  • Array is also used for zenity approach.

  • "Quit" option is not among the initial, original options. It is "added" when needed, so your array stay clean. Afterall, "Quit" is not needed for zenity anyway, user can just click "Cancel" (or close the window) to exit. Notice how both uses the same, untouched array of options.

  • PS3 and REPLY vars can not be renamed. select is hardcoded to use those. All other variables in script (opt, options, prompt, title) can have any names you want, provided you do the adjustments

share|improve this answer
    
Awesome explanation. Thank you. This question still ranks high on Google, so too bad it is closed. –  MountainX Jun 30 '13 at 8:44
add comment

Since this is targeted at Ubuntu you should use whatever backend debconf is configured to use. You can find out the debconf backend with:

sudo -s "echo get debconf/frontend | debconf-communicate"

If it says "dialog" then it likely uses whiptail or dialog. On Lucid it's whiptail.

If that fails, use bash "select" as explained by Dennis Williamson.

share|improve this answer
    
This is probably overkill for that question, but +1 for mentioning whiptail and dialog! I wasnt aware of those commands... very sweet! –  MestreLion Aug 5 '11 at 5:58
add comment

You can use this simple script for creating options

!#/bin/bash
echo "select the operation *********  1)operation 1 2)operation 2 3)operation 3 4)operation 4 "

read n
case $n in
    1) commands for opn 1;;
    2) commands for opn 2;;
    3) commands for opn 3;;
    4) commands fo  opn 4;;
    *) invalid option;;
esac
share|improve this answer
add comment
#!/bin/sh
show_menu(){
    NORMAL=`echo "\033[m"`
    MENU=`echo "\033[36m"` #Blue
    NUMBER=`echo "\033[33m"` #yellow
    FGRED=`echo "\033[41m"`
    RED_TEXT=`echo "\033[31m"`
    ENTER_LINE=`echo "\033[33m"`
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${MENU}**${NUMBER} 1)${MENU} Mount dropbox ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 2)${MENU} Mount USB 500 Gig Drive ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 3)${MENU} Restart Apache ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 4)${MENU} ssh Frost TomCat Server ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 5)${MENU} ${NORMAL}"
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}"
    read opt
}
function option_picked() {
    COLOR='\033[01;31m' # bold red
    RESET='\033[00;00m' # normal white
    MESSAGE=${@:-"${RESET}Error: No message passed"}
    echo -e "${COLOR}${MESSAGE}${RESET}"
}

clear
show_menu
while [ opt != '' ]
    do
    if [[ $opt = "" ]]; then 
            exit;
    else
        case $opt in
        1) clear;
        option_picked "Option 1 Picked";
        sudo mount /dev/sdh1 /mnt/DropBox/; #The 3 terabyte
        menu;
        ;;

        2) clear;
            option_picked "Option 2 Picked";
            sudo mount /dev/sdi1 /mnt/usbDrive; #The 500 gig drive
        menu;
            ;;

        3) clear;
            option_picked "Option 3 Picked";
        sudo service apache2 restart;
            show_menu;
            ;;

        4) clear;
            option_picked "Option 4 Picked";
        ssh lmesser@ -p 2010;
            show_menu;
            ;;

        x)exit;
        ;;

        \n)exit;
        ;;

        *)clear;
        option_picked "Pick an option from the menu";
        show_menu;
        ;;
    esac
fi
done
share|improve this answer
add comment

There is already the same question in serverfault answered. The solution there uses whiptail.

share|improve this answer
    
Thanks, but as my script is for mainstream consumption, I don't want it to have any additional dependecies. But I'll bookmark that for use in the future, who knows. –  Daniel Rodrigues Aug 8 '10 at 22:01
add comment

I have used Zenity, which seems always there in Ubuntu, works very well and has many capabilities. This is a sketch of a possible menu:

#! /bin/bash

selection=$(zenity --list "Option 1" "Option 2" "Option 3" --column="" --text="Text above column(s)" --title="My menu")

case "$selection" in
"Option 1")zenity --info --text="Do something here for No1";;
"Option 2")zenity --info --text="Do something here for No2";;
"Option 3")zenity --info --text="Do something here for No3";;
esac
share|improve this answer
    
Oops! sorry about the appearance of this snippet, first time posting and seems have to turn HTML off perhaps –  Mike Birchley May 5 '11 at 1:36
    
Better, turned 'code sample' on in editing, sorry about that –  Mike Birchley May 5 '11 at 1:47
add comment

Have a look at whiptail (Ubuntu package) and the bash guide.

share|improve this answer
1  
The so-called advanced bash scripting guide is not a good guide for learning bash. It teaches you to write bugs, not scripts. mywiki.wooledge.org/BashGuide is suitable for beginners as well as more experienced users, and more importantly it teaches good practice (the only guide I know of). –  geirha Feb 3 '11 at 23:33
    
geira, I made it 1/3 the way through mywiki.wooledge.org/BashGuide/Patterns and started finding errors. I think before recommending people use that guide and not the Advanced Bash Scripting Guide you should either correct the errors (I assume you have something to do with the guide since you're promoting it). This does actually work even if bad form - for file in ls; do rm "$file"; done –  user97651 Oct 14 '12 at 18:29
add comment

Assuming you want to use a plain shell script menu (no fancy UI), check the menu example from http://www.tldp.org/LDP/abs/html/testbranch.html .

share|improve this answer
    
As with much of the other snippets in the advanced bash-scripting guide, those snippets contain bugs and bad practice. –  geirha Feb 3 '11 at 23:39
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.