Creating user friendly interface with dialog command

Categories: bash

In this article, we will see how to use “dialog” command to create a user-friendly interface for your bash scripts. dialog command helps us build text-based front-end windows. Still confused about what i am talking about exactly? The below image makes it all clear. We are gonna learn how to make such windows (there are various kinds of them).

I am gonna show you how to create such windows and how to integrate them into your bash script. Following are various different types of dialog box:

    >> calendar, checklist, form, fselect, gauge, infobox, inputbox, inputmenu, menu, msgbox, password, pause, radiolist, tailbox, tailboxbg, textbox, timebox, and yesno.

we are gonna cover some of the basic one’s.
Tip Tip : Mostly the random numbers which you see behind an dialog command are nothing but height and width. heh. :P
msgbox
The image at top of this post is an pure example of an “message box”

$ dialog --title "This is a message box title" \
--msgbox "Hello, this is the message box text." 10 30

yesno

dialog --title "www.anildewani.com" \
        --yesno "Are you gonna press yes or not ?" 5 40

case $? in
0)
        echo "Yes! You pressed Yes";;
1)
        echo "No! You pressed No";;
255)
        echo "Esc? Thats cool with me";;
esac

That would look something like :

Inputbox
Inputbox option is used to read input from user. It mostly prints out instantly upon submission to the stderr. Following is an sample bash script which probably redirects the stderr to an temp file and store the input.

dialog --title "www.anildewani.com" \
        --inputbox "Enter your name..." 10 40 2>/tmp/dialog

case $? in
0)
        echo "Welcome `cat /tmp/dialog`";;
1)
        echo "Cancel was pressed";;
esac

That’s how it shall look like:

Textbox
Textbox option is used to show contents of an file into an window. User can even scroll through and use various keys like pageup and pagedown. User can even search by pressing /

dialog --title "www.anildewani.com" \
 --textbox file.txt 20 50

It will display contents of file.txt in following manner:

Checklist

dialog --title "www.anildewani.com" \
        --checklist "Select the OS you use.." 20 40 3 \
        "CentOS" "" on \
        "Ubuntu" "" off \
        "Fedora" "" off 2>/tmp/dialog

case $? in
0)
        echo "Glad to know you use `cat /tmp/dialog`";;
1)
        echo "Cancel was pressed";;
esac

In above example, 20 is height, 40 is the width and 3 is total number of choices.
Below is image of how this should look like:

Radiolist
Just replace –checklist in checklist example script to –radiolist. Rest is all the same :)

Guage
Guage window can be used to show a progress bar showing the progress of your program. Lets look at an simple example.

{
for i in {1..100}
do
        echo $i
        sleep 1
done
echo;
} | dialog --guage "Loading www.anildewani.com ..." 7 70 0

The dialog commands gets input from code between parenthesis. That’s the tricky part. ;)
Here’s the screenshot of how it looks like:

Will update with more when time allows me :)

Tags: ,
No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>