Quantcast
Viewing latest article 3
Browse Latest Browse All 14

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 =).


Posted in Linux, Shell Script Tagged: Dialog, Linux, pdmenu, Shell Script Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 3
Browse Latest Browse All 14

Trending Articles