Creating dialog boxes with the dialog utility in Linux bash script

May 5, 2021

Comments

Adsense

Adsense

Adsense

Adsense

The dialog utility is used to create a basic-level graphical user interface. We can use this in shell script to create very useful programs.

To install the dialog utility in Debian or Ubuntu Linux, enter the following command:

$ sudo apt-get update
$ sudo apt-get install l dialog

Similarly, enter the following command to install the utility dialog in CentOS or Red Hat Linux:

$ sudo yum install dialog

The typical syntax of the dialog command is as follows:

$ dialog --common-options --boxType "Text" Height Width                                   --box-specific-option

The common-options utility is used to set the background color, title, and so on in dialog boxes.

The option details are as follows:

  • Text: The caption or contents of the box
  • Height: The height of the dialog box
  • Width: The width of the dialog box

Creating a message box (msgbox)

To create a simple message box, we can use the following command:

$ dialog --msgbox "This is a message." 10 50
Dialog box Linux Concept

Creating a message box (msgbox) with a title

Enter the following command to create a message box with the following title:

$ dialog --title "Hello" --msgbox 'Hello world!' 6 20

The option details are as follows:

  • --title "Hello": This will set the title of the message box to "Hello"
  • --msgbox 'Hello world!': This will set the content of the message box to "Hello world!"
  • 6: This will set the height of the message box
  • 20: This will set the width of message box:
Message Box Linux Concept

The message box has a Hello title with content Hello World! It has a single OK button. We can use this message box to inform the user about any events or information. The user will have to press Enter to close this message box. If the content is large for a message box, then the dialog utility will provide the scrolling of the message.

The yes/no box (yesno)

If we need to obtain a yes or no answer from the user, we can use the following options along with the dialog command:

$ dialog --yesno "Would you like to continue?" 10 50
Dialog Box image Linux Concept

We can have the same yes/no dialog box with a title as follows:

$ dialog --title "yesno box" --yesno "Would you like to continue?" 10 50
Dialog Box image Linux Concept

Let’s write the dialog_01.sh shell script as follows:

dialog_01.sh

#!/bin/bash 
dialog --title "Delete file"  
--backtitle "Learning Dialog Yes-No box"  
--yesno "Do you want to delete file "~/work/sample.txt"?" 7 60 
 
# Selecting "Yes" button will return 0. 
# Selecting "No" button will return 1. 
# Selecting [Esc] will return 255. 
result=$? 
case $result in 
   0)     rm ~/work/sample.txt 
    echo "File deleted.";; 
   1)     echo "File could not be deleted.";; 
   255)   echo "Action Cancelled - Presssed [ESC] key.";; 
esac 

Let’s test the following program:

$ chmod +x dialog_01.sh
$ ./dialog_01.sh

Output:

Dialog box yes no image

The input box (inputbox)

Whenever we want to ask a user to input text via the keyboard, in such situations, the inputbox option is useful. While entering text via the keyboard, we can use keys such as Delete, Backspace, and the arrow cursor keys for editing. If the input text is larger than the input box, the input field will be scrolled. Once the OK button is pressed, the input text can be redirected to a text file:

# dialog  --inputbox  "Please enter something."   10  50 2> /tmp/tempfileVAR=`cat ~/work/output.txt
Input Dialog box image Linux Concept

Let’s write the dialog_02.sh shell script to create an input box as follows:

dialog_02.sh

#!/bin/bash 
result="output.txt" 
>$ $result    # Create empty file 
dialog --title "Inputbox Demo"  
--backtitle "Learn Shell Scripting"  
--inputbox "Please enter your name " 8 60 2>$result 
 
response=$? 
name=$(<$result) 
case $response in 
0)   echo "Hello $name" 
      ;; 
1)   echo "Cancelled." 
      ;; 
255)     echo "Escape key pressed." 
esac 
rm $result 

Let’s test the following program:

$ chmod +x dialog_02.sh
$ ./dialog_02.sh
dialog box image Linux Concept

Output:

Output:

"Hello Ganesh Naik"

The textbox (textbox)

If we want to display the contents of the file in a textbox inside the menu created by a dialog, then enter the following command:

$ dialog  --textbox /etc/passwd 10  50
Text Dialog Box Image Linux Concept

We are displaying the /etc/passwd file in the textbox with the previous command.

A password box

Many times, we need a password from the user. In this case, the password should not be visible on the screen. The password box option is perfect for this purpose.

If we want to display an entered password as a string of ****, then we will need to add the --insecure option.

We will need to redirect the inserted password to a file.

Let’s write dialog_03.sh shell script to receive the password as follows:

dialog_03.sh

#!/bin/bash 
# creating the file to store password 
result="output.txt 2>/dev/null" 
 
# delete the password stored file, if program is exited pre-maturely. 
trap "rm -f output.txt" 2 15 
 
dialog --title "Password"  
--insecure  
--clear  
--passwordbox "Please enter password" 10 30 2> $result 
 
reply=$? 
 
case $reply in 
  0)    echo "You have entered Password :  $(cat $result)";; 
  1)    echo "You have pressed Cancel";; 
  255)    cat $data && [ -s $data ] || echo "Escape key is pressed.";; 
esac 

Let’s test the following program:

$ chmod +x dialog_03.sh
$ ./dialog_03.sh

Output:

Output:

You have entered Password :  adcd1234

The checklist box (checklist)

In this case, we can present the user with a choice to select one or multiple options from a list:

# dialog --checklist "This is a checklist" 10 50 2  
"a" "This is one option" "off"  
"b" "This is the second option" "on" 
Checklist Dialog Box Image Linux Concept

The menu box (menu)

Usually, a program or shell script may be required to perform multiple types of tasks. In such cases, the menu box option is very useful. This option will display a list of choices for the user. Then, the user may select an option of their own choice. Our script should execute the desired option.

Each menu has two fields, a tag and an item string. In the next example menu demo, we have tags such as date, calendar, and editor. A description of a tag is called an item string.

Let’s write the dialog_04.sh shell script to create a menu as follows:

dialog_04.sh

#!/bin/bash 
# Declare file to store selected menu option 
RESPONSE=menu.txt 
# Declare file to store content to display date and cal output 
TEMP_DATA=output.txt 
vi_editor=vi 
# trap and delete temp files 
trap "rm $TEMP_DATA; rm $RESPONSE; exit" SIGHUP SIGINT SIGTERM 
 
function display_output(){ 
    dialog --backtitle "Learning Shell Scripting" --title "Output" --clear --msgbox "$(<$TEMP_DATA)" 10 41 
} 
 
function display_date(){ 
    echo "Today is `date` @ $(hostname -f)." >$TEMP_DATA 
    display_output 6 60 "Date and Time" 
} 
 
function display_calendar(){ 
    cal >$TEMP_DATA 
    display_output 13 25 "Calendar" 
} 
 
# We are calling infinite loop here 
while true 
do 
 
# Show main menu 
dialog --help-button --clear --backtitle "Learn Shell Scripting"  
--title "[ Demo Menubox ]"  
--menu "Please use up/down arrow keys, number keysn 
1,2,3.., or the first character of choicen 
as hot key to select an option" 15 50 4  
Calendar "Show the Calendar"  
Date/time "Show date and time"  
Editor "Start vi editor"  
Exit "Terminate the Script" 2>"${RESPONSE}" 
 
menuitem=$(<"${RESPONSE}") 
 
# Start activity as per selected choice 
case $menuitem in 
    Calendar) display_calendar;; 
    Date/time) display_date;; 
    Editor) $vi_editor;; 
    Exit) echo "Thank you !"; break;; 
esac 
done 
# Delete temporary files 
[ -f $TEMP_DATA ] && rm $TEMP_DATA 
[ -f $RESPONSE ] && rm $RESPONSE 

Let’s test the following program:

$ chmod +x dialog_04.sh
$ ./dialog_04.sh

Output:

Menu Dialog Box Image Linux Concept

The radiolist box (radiolist)

If you want the user to select only one option out of many choices, then radiolist is a suitable option:

# dialog --radiolist  "This is a selective list, where only one  
option can be chosen" 10 50 2  
"a" "This is the first option" "off"  
"b" "This is the second option" "on" 

Radio buttons are not square but round, as can be seen in the following screenshot:

Redio Dialog Box Image Linux Concept

The progress meter box (gauge)

The progress meter displays a meter at the bottom of the box. This meter indicates the percentage of the process completed. New percentages are read from standard input, one integer per line. This meter is updated to reflect each new percentage.

Let’s write the dialog_05.sh shell script to create a progress meter as follows:

dialog_05.sh

#!/bin/bash 
declare -i COUNTER=1 
{ 
    while test $COUNTER -le 100 
        do 
            echo $COUNTER 
            COUNTER=COUNTER+1 
            sleep 1 
    done 
    } |  dialog --gauge  "This is a progress bar"  10 50 0

Let’s test the following program:

$ chmod +x dialog_05.sh
$ ./dialog_05.sh

Output:

Progress Bar Dialog Box Image Linux Concept

Customization of dialog with the configuration file.

We can customize dialog using the ~/.dialogrc configuration file. The default file location is $HOME/.dialogrc.

To create the .dialogrc configuration file, enter the following command:

$ dialog --create-rc ~/.dialogrc

We can customize the output of the dialog utility by changing any of the configuration parameters defined in the .dialogrc file.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Related Posts

Backup of files in Linux using command line

In IT or our day-to-day computer industry activities, taking backup is one of the most important activities. Previously, offices were required to keep important paper in a safe place; but if a fire breaks out, then everything is finished. In the digital world, taking...

Using awk in Linux

awk is a program that has its own programming language for performing data-processing and generating reports. The GNU version of awk is gawk. awk processes data, which can be received from a standard input, input file, or as the output of any other command or process....

0 Comments

0 Comments

Submit a Comment

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

fourteen − twelve =

News & Updates

Join Our Newsletter

Adsense

Adsense

Adsense

Adsense