Dialog – Create a Simple Menu for Inexperienced Linux Users

Recently, i have a new task which is adding a user account on a Linux box with UI login shell. My project leader suggests a few tools for me to study and finally i pick the the one called pdmenu.

pdmenu is based on dialog command in Linux. So before talking about pdmenu, i would like to show you how to use dialog.

Bash display dialog boxes

you can install dialog to create simple message box in command console. For Ubuntu user, just apt-get dialog.

1. Create a Hello World message box

dialog --title "Hello" --msgbox 'Hello world!' 6 20
  • title – Title of the message box
  • msgbox – The text in the message box
  • 6 20 – The size of the message box





2. Create a Yes/No message box

dialog --title "Message"  --yesno "Are you OK?" 6 25

The box return 0 if yes is chosen and 1 if no.




3. Create a simple menu

 dialog --menu "Menu" 10 30 3 1 red 2 green 3 blue 2>temp

Similar to yes/no box, it return 0 if OK and 1 if Cancel. And the selection (1, 2, 3) will be printed in stdout which is direct to a file named as “temp”.



4. Example
The following is a shell script example which help u to list file in /home, /root or /tmp directory.

#!/bin/sh
dialog --menu "List file of a directory" 10 30 3 1 /home 2 /root 3 /tmp 2>temp


# OK is pressed
if [ "$?" = "0" ]
then
        _return=$(cat temp)

        # /home is selected
        if [ "$_return" = "1" ]
        then
                dialog --title "List file of directory /home" --msgbox "$(ls /home -l)" 100 100
        fi

         # /root is selected
        if [ "$_return" = "2" ]
        then
                dialog --title "List file of directory /root" --msgbox "$(ls /root -l)" 100 100
        fi

         # /tmp is selected
        if [ "$_return" = "3" ]
        then
                dialog --title "List file of directory /tmp" --msgbox "$(ls /tmp -l)" 100 100
        fi

# Cancel is pressed
else
        echo "Cancel is pressed"
fi

# remove the temp file
rm -f temp

There are other examples which can be found at Dialog: An Introductory Tutorial

Done and I will talk about pdmenu later =).

Advertisements

5 thoughts on “Dialog – Create a Simple Menu for Inexperienced Linux Users”

  1. Hi there , i have a dunnos

    Well its need it

    #!/bin/sh
    dialog --menu "Some Title" Size1 Size2 (I dont understand the follow ->) 3 1 /home 2 /root 3 /tmp 2>temp (send to temp file?)
    
    # OK is pressed
    if [ "$?" = "0" ]
    

    Read Values from ???

    Okey i just trying to understand

    well i wanna make a Dialog GUI Selector , and when i choise some option , this in the process show a gauge / progress bar .

    I put here the script i try to do ->

    #!/bin/bash
    DIALOG=${DIALOG=dialog}
    Title="Wine Manual Installer"
    Question="Please Choise a Version : "
    
    Versions=( 
    "1.6.2"
    "1.7.0"
    "1.7.1"
    "1.7.2"
    "1.7.3"
    "1.7.4"
    "1.7.5"
    "1.7.6"
    "1.7.7"
    "1.7.8"
    "1.7.9"
    "1.7.10"
    "1.7.11"
    "1.7.12"
    "1.7.13"
    "1.7.14"
    "1.7.15"
    "1.7.16"
    "1.7.17"
    "1.7.18"
    "Salir"
    )
    Versions=( ${Versions[@]/#/"FALSE "} )
    
    while true; do
    	Opcion="$( $DIALOG --clear --title "$Title" \
    	--menu "Wine:" 20 51 4 \
    	"$Versions" "$Versions" \ ) "
    
          if   [ "$Opcion" = "1.6.2" ]; then
               dialog --msgbox "You has been choise $Option, Option 01" 0 0
          elif [ "$Opcion" = "1.7.0" ]; then
               dialog --msgbox "You has been choise $Option, Option 02" 0 0
          elif [ "$Opcion" = "1.7.1" ]; then
               dialog --msgbox "You has been choise $Option, Option 03" 0 0
          elif [ "$Opcion" = "1.7.2" ]; then
               dialog --msgbox "You has been choise $Option, Option 04" 0 0
          elif [ "$Opcion" = "1.7.3" ]; then
               dialog --msgbox "You has been choise $Option, Option 05" 0 0
          elif [ "$Opcion" = "1.7.4" ]; then
               dialog --msgbox "You has been choise $Option, Option 06" 0 0
          elif [ "$Opcion" = "1.7.5" ]; then
               dialog --msgbox "You has been choise $Option, Option 07" 0 0
          elif [ "$Opcion" = "1.7.6" ]; then
               dialog --msgbox "You has been choise $Option, Option 08" 0 0
          elif [ "$Opcion" = "1.7.7" ]; then
               dialog --msgbox "You has been choise $Option, Option 09" 0 0
          elif [ "$Opcion" = "1.7.8" ]; then
               dialog --msgbox "You has been choise $Option, Option 10" 0 0
          elif [ "$Opcion" = "1.7.9" ]; then
               dialog --msgbox "You has been choise $Option, Option 11" 0 0
          elif [ "$Opcion" = "1.7.10" ]; then
               dialog --msgbox "You has been choise $Option, Option 12" 0 0
          elif [ "$Opcion" = "1.7.11" ]; then
               dialog --msgbox "You has been choise $Option, Option 13" 0 0
          elif [ "$Opcion" = "1.7.12" ]; then
               dialog --msgbox "You has been choise $Option, Option 14" 0 0
          elif [ "$Opcion" = "1.7.13" ]; then
               dialog --msgbox "You has been choise $Option, Option 15" 0 0
          elif [ "$Opcion" = "1.7.14" ]; then
               dialog --msgbox "You has been choise $Option, Option 16" 0 0
          elif [ "$Opcion" = "1.7.15" ]; then
               dialog --msgbox "You has been choise $Option, Option 17" 0 0
          elif [ "$Opcion" = "1.7.16" ]; then
               dialog --msgbox "You has been choise $Option, Option 18" 0 0
          elif [ "$Opcion" = "1.7.17" ]; then
               dialog --msgbox "You has been choise $Option, Option 19" 0 0
          elif [ "$Opcion" = "1.7.18" ]; then
               dialog --msgbox "You has been choise $Option, Option 20" 0 0
          elif [ "$Opcion" = "Salir" ]; then
             exit 1
             else
                           dialog --msgbox "You has been choise $Option, Invalid Option"
             fi
    done
    

    Like

    1. dialog --menu "Some Title" Size1 Size2 3(if u have 3 choices, set it to 3) 1 /home 2 /root 3 /tmp 2>temp (send to temp file? yes)
      

      The dialog box will have 2 buttons. If the user select OK, 0 is returned.

      Like

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.