Home > bash > Whiptail – Using a menu

Whiptail – Using a menu

Problem: I needed a menu.

Solution: Here is a sample of what I went with.

#! /bin/bash
whiptail --title "Backups" --yesno --defaultno "Start the sample program." 8 78

exitstatus=$?
if [ $exitstatus = 0 ]; then
    status="0"
    while [ "$status" -eq 0 ]  
    do
        choice=$(whiptail --title "Testing" --menu "Make a choice" 16 78 5 \
        "Application 1" "Start backing up the first application." \
        "Application 2" "Start backing up the second application." 3>&2 2>&1 1>&3) 
        
        # Change to lower case and remove spaces.
        option=$(echo $choice | tr '[:upper:]' '[:lower:]' | sed 's/ //g')
        case "${option}" in 
            application1) 
                whiptail --title "Testing" --msgbox "In first option" 8 78
            ;;
            application2)
                whiptail --title "Testing" --msgbox "In second option" 8 78
            ;;
            *) whiptail --title "Testing" --msgbox "You cancelled or have finished." 8 78
                status=1
                exit
            ;;
        esac
        exitstatus1=$status1
    done
else
    whiptail --title "Testing" --msgbox "You chose not to start." 8 78
    exit 
fi

Whiptail Menu example

Advertisements
  1. May 12, 2017 at 11:33 am

    Greetings,
    This is an awesome structure.
    What is “3>&2 2>&1 1>&3” all about in line 12?
    Regards,
    Flint@flint.com

    • May 12, 2017 at 4:17 pm

      As per the man page

             --menu text height width menu-height [ tag item ] ...
                    As  its name suggests, a menu box is a dialog box that can be used to present a list of choices in the form of a menu for the user to choose. Each menu entry
                    consists of a tag string and an item string. The tag gives the entry a name to distinguish it from the other entries  in  the  menu.  The  item  is  a  short
                    description  of the option that the entry represents. The user can move between the menu entries by pressing the UP/DOWN keys, the first letter of the tag as
                    a hot-key. There are menu-height entries displayed in the menu at one time, but the menu will be scrolled if there are more entries than that. When  whiptail
                    exits, the tag of the chosen menu entry will be printed on stderr.
      

      From what I have in the code below, I need to capture the users choice and proceed based on that.

      choice=$(whiptail --title "Testing" --menu "Make a choice" 16 78 5 \
              "Application 1" "Start backing up the first application." \
              "Application 2" "Start backing up the second application." 3>&2 2>&1 1>&3) 
               
              # Change to lower case and remove spaces.
              option=$(echo $choice | tr '[:upper:]' '[:lower:]' | sed 's/ //g')
      

      Saving the choice into a variable is hard to do since whiptail is already using stdin and stdout. One way to do this is to redirect stderr to stdout.

      The below links should provide a bit more information about how this is possible.

      http://www.tldp.org/LDP/abs/html/io-redirection.html

      http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

      http://stackoverflow.com/questions/1970180/whiptail-how-to-redirect-output-to-environment-variable#1970254

      http://www.guru99.com/linux-redirection.html

      I hope that helps 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: