Dialog Utility 11.1 Install
Dialog Utility 11.1 Install
Dialog Utility 11.1 Install
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.
11.2 Infobox
After installation you can start to use dialog utility. Before understanding the syntax of
dialog utility try the following script:
$ 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. .
."
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.
$ chmod +x dia2
$ ./dia2
Example:
#!/bin/bash
echo "enter name"
read name
dialog --title "Example Dialog message box" --msgbox "Hello $name" 6 50
$ 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,
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:
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`.
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
$ chmod +x smenu
$ ./smenu
--menu option is used of dialog utility to create menus, menu option take
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:
11.9 Infobox:
$ dialog --infobox "Processing, please wait" 3 34 ; sleep 5
11.10 Textbox:
It is a simple file viewer
case $? in
0)
echo "You have entered: $dat" ;;
1)
echo "You have pressed Cancel" ;;
255)
echo "Box closed" ;;
esac