Dialog Utility 11.1 Install

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

PART 11

Dialog utility
11.1 Install
Before programming using dialog utility you need to install the dialog utility, since dialog
utility in not installed by default.

To install 'dialog' on your ubuntu:


$ apt-get install dialog

11.2 Infobox
After installation you can start to use dialog utility. Before understanding the syntax of
dialog utility try the following script:

dialog --title "Linux Dialog Utility Infobox" --backtitle


"Linux Shell Script Tutorial" --infobox "This is dialog box
called infobox, which is used to show some information on
screen, Thanks to Savio Lam and Stuart Herbert to give us
this utility. Press any key. . . " 7 50 ; read

Save the shell script and run it as:

$ chmod +x dia1
$ ./dia1
After executing this dialog statement you will see box on screen with titled as

"Welcome to Linux Dialog Utility" and message "This is dialog....Press any key. .
."

inside this box.

 The title of box is specified by --title option and infobox with --infobox "Message"
with this option.
 Here 7 and 50 are height-of-box and width-of-box respectively.
 "Linux Shell Script Tutorial" is the backtitle of dialog show on upper left side of
screen and below that line is drawn.
 Use dialog utility to Display dialog boxes from shell scripts.

11.3 Message box (msgbox)

dialog --title "Linux Dialog Utility Msgbox" --backtitle "Linux Shell


Script Tutorial" --msgbox "This is dialog box called msgbox, which is used
to show some information on screen which has also Ok button, Thanks to
Savio Lam and Stuart Herbert to give us this utility. Press any key. . . "
9 50

Save it and run as

$ chmod +x dia2
$ ./dia2

Example:

#!/bin/bash
echo "enter name"
read name
dialog --title "Example Dialog message box" --msgbox "Hello $name" 6 50

11.4 yesno box


dialog --title "Alert : Delete File" --backtitle "Linux Shell
Script Tutorial" --yesno "\nDo you want to delete
'/usr/letters/jobapplication' file" 7 60
sel=$?
case $sel in
0) echo "User select to delete file";;
1) echo "User select not to delete file";;
255) echo "Canceled by user by pressing [ESC] key";;
esac

Save the script and run it as:

$ chmod +x dia3
$ ./dia3

Above script creates yesno type dialog box, which is used to ask some questions to the
user , and answer to those question either yes or no.

After asking question how do we know, whether user has press yes or no button ? The
answer is exit status,

 if user press yes button exit status will be zero,


 if user press no button exit status will be one and
 if user press Escape key to cancel dialog box exit status will be one 255.
That is what we have tested in our above shell script as

Statement Meaning
sel=$? Get exit status of dialog utility
Now take action according to
case $sel in exit status of dialog utility,
0) echo "You select to delete file";;
if exit status is 0 , delete file,
1) echo "You select not to delete file";;
if exit status is 1 do not delete
255) echo "Canceled by you by pressing [Escape] key";; file and
esac if exit status is 255, means
Escape key is pressed.

Example:

$ dialog --title "Confirmation" --yesno "Want to quit?" 6 20

11.5 Input Box (inputbox)


dialog --title "Inputbox - To take input from you" --backtitle "Linux Shell
Script Tutorial" --inputbox "Enter your name please" 8 60 2>/tmp/input.txt

sel=$?

na=`cat /tmp/input.txt`
case $sel in
0) echo "Hello $na" ;;
1) echo "Cancel is Press" ;;
255) echo "[ESCAPE] key pressed" ;;
esac

rm -f /tmp/input.txt

Run it as follows:

$ chmod +x dia4
$ ./dia4
Inputbox is used to take input from user, In this example we are taking Name of user
as input. But where we are going to store inputted name, the answer is to redirect
inputted name to file via statement

2>/tmp/input.txt

at the end of dialog command, which means send screen output to file
called /tmp/input.txt letter we can retrieve this inputted name and store to variable as
follows

na=`cat /tmp/input.txt`.

For input box's exit status refer the following table:

Exit Status for


Meaning
Input box
0 Command is successful
1 Cancel button is pressed by user
255 Escape key is pressed by user

11.6 Menu Box (menu)


Its time to write script to create menus using dialog utility, following are menu items and
action for each menu-item is follows :

MENU-ITEM ACTION
Date/time Show current date/time
Calendar Show calendar
Editor Start nano Editor

#
#How to create small menu using dialog
#
dialog --backtitle "Linux Shell Script Tutorial " --title "Main \
Menu" --menu "Move using [UP] [DOWN],[Enter] to \
Select" 15 50 3\
Date/time "Shows Date and Time" \
Calendar "To see calendar " \
Editor "To start nano editor " 2>/tmp/menuitem.txt

menuitem=`cat /tmp/menuitem.txt`

opt=$?

case $menuitem in
Date/time) date;;
Calendar) cal;;
Editor) vi;;
esac

Save it and run as:

$ chmod +x smenu
$ ./smenu
--menu option is used of dialog utility to create menus, menu option take

--menu options Meaning


"Move using [UP] [DOWN],[Enter]
This is text show before menu
to Select"
15 Height of box
50 Width of box
3 Height of menu
First menu item called as tag1 (i.e. Date/time) and
Date/time "Shows Date and Time" description for menu item called
as item1 (i.e. "Shows Date and Time")
First menu item called as tag2 (i.e. Calendar) and
description for menu item called as item2 (i.e. "To
Calendar "To see calendar "
see calendar")
First menu item called as tag3 (i.e. Editor) and
Editor "To start nano editor " description for menu item called as item3 (i.e."To
start nano editor")
Send selected menu item (tag) to this temporary
2>/tmp/menuitem.txt
file

After creating menus, user selects menu-item by pressing the ENTER key, selected
choice is redirected to temporary file, Next this menu-item is retrieved from temporary
file and following case statement compare the menu-item and takes appropriate step
according to selected menu item.
As you see, dialog utility allows more powerful user interaction then the older read and
echo statement.

Example:

$ dialog --title "A dialog Menu Example" \


--menu "Please choose an option:" 15 55 5 \
1 "Add a record to DB" \
2 "Delete a record from DB" \
3 "Exit from this menu"

11.7 Checklist box


A checklist box allows you to present a set of choices to the user and the user can toggle
each one on or off individually using the space bar.
A sample one:

$ dialog --checklist "Choose OS:" 15 40 5 \


1 Linux off \
2 Solaris on \
3 'HP UX' off \
4 AIX off
11.8 Radiolist box:
The 'radiolist' control box is same as 'checklist' box.

$ dialog --backtitle "OS infomration" \


--radiolist "Select OS:" 10 40 3 \
1 "Linux 7.2" off \
2 "Solaris 9" on \
3 "HPUX 11i" off

11.9 Infobox:
$ dialog --infobox "Processing, please wait" 3 34 ; sleep 5
11.10 Textbox:
It is a simple file viewer

$ dialog --textbox ~/work/conf.txt 10 40

11.11 Gauge Box:


#!/bin/sh
#A gauge Box example with dialog
(
c=0
while [ $c -le 100 ]
do
echo $c
echo "###"
echo "$c %"
echo "###"
c=$((c+10))
sleep 1
done
) |
dialog --title "A Test Gauge With dialog" --gauge "Please wait ...." 10 60 0

11.12 Calendar Box:


#!/bin/sh

dat=$(dialog --stdout --title "My Calendar" \


--calendar "Select a date:" 0 0 25 12 2009)

case $? in
0)
echo "You have entered: $dat" ;;
1)
echo "You have pressed Cancel" ;;
255)
echo "Box closed" ;;
esac

11.13 Time Box:


#!/bin/sh

tim=$(dialog --stdout --title "A TimeBox" \


--timebox "Set the time:" 0 0 10 13 59)
case $? in
0)
echo "You have set: $tim" ;;
1)
echo "You have pressed Cancel" ;;
255)
echo "Box closed" ;;
esac

You might also like