Open In App

Shell Scripting – Dialog Boxes

Last Updated : 10 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will create a shell script that generates a Dialog Box with GUI generating a message to the user. 

What is a Dialog Box? 

A Dialog Box is a temporary window an application runs to convey important information to the users. These dialog boxes can be used to display warnings, errors, and also any additional inputs needed by the system from End Users.

Installation Of Dialog Utility In Linux  : 

Dialog utility: Dialog Utility is used to create a Graphical User Interface-based Dialog Box that can be used to get any important information from the users. 

Modules Needed To Be Installed : 

For Ubuntu and Debian Linux 

sudo apt-get update 
sudo apt-get install 1 dialog 

For CentOS or Redhat Linux : 

sudo yum install dialog

Now, Let us run Bash Scripts to create Dialog Boxes, please do note that we are using an ubuntu-based Linux VM, but you can try this on any Linux Distribution but please follow the above-mentioned commands for respective distribution to download modules. 

1. Shell Script to create Dialog Box to Greet Users : 

In this, let’s create a shell script that generates a greeting to users. 

Shell Script : 

#!/bin/bash 
function DialogGen() { 
dialog --title "Hello" --msgbox 'Hello GFG Users!' 6 20 
}
DialogGen

Output : 

  • In Above Shell Script we generated a Dialog Box on a Ubuntu Machine that Greeted users using the DialogGen function. It displayed a message box greeting “Hello GFG Users” with an ok button. 
  • We can use this dialog box to display any message of our choice. 
  • Users can click on the ok button and the dialog box exits and the calling shell script continues its operation. 
  • After exiting, there will be no text written on dialog’s output but an ESC type exit status will be returned. 

Understanding options Involved in Dialog Box 

Now, In the above-created dialog Box let us debug the options used for getting a better understanding of how Dialog Boxes function on a Linux Environment. 

  • –title “Hello”: This sets a title string that gets displayed at the beginning of the dialog box which acts as the title for created Dialog Box. 
  • –msgbox ‘Hello GFG Users !’: This displays a message “Hello” to users, you can customize what you want to greet end-users. 
  • 6: value for Height of msgbox box.
  • 20: Value of width of msgbox box. 

Now, let’s go through various widgets that can be created using the Dialog tool using shell scripts. 

2. Shell Script to display Dialog Box with Calendar : 

Syntax : 

-checklist  <text> <height> <width> <list height> <tag1> <item1> <status1>…

messg.sh : 

#!/bin/bash 
function DialogGen 
{ 
dialog --calendar "calendar" 5 50 12 02 2022 
}
DialogGen 

Output : 

Understanding options used in the above dialog box : 

  • –calendar: This Keyword is used to display the title of the Dialog Box. 
  • –5 50 12 02 2022 : 5 is value for Height 50 is for width, 12 02 2022 is today’s date. 

3. Shell Script to generate Dialog Box for displaying checklist widget. 

Let’s create a shell script that generated a checklist of fruits and vegetables : 

messg.sh : 

#!/bin/bash 
function DialogGen ()
{
    dialog --checklist 'checklist' 15 10 10 'potato'  5 'on'  'carrot' 2 'off' 'grape' 3 'on' 'cabbage; 4 'off' 
} 
DialogGen

Output : 

Understanding options used in the above dialog box :  

  • –Checklist “checklist”: Checklist keyword initiates checklist followed by text that used as the title of the Dialog Box. 
  • –15 10 10: 15 in height followed by 10 value set for width and 10 used as the value set for list height. 
  • –‘potato’ 5 ‘on’: here potato is item1 is potato followed by 5 set as tag value and on is the status for checklist box(it is either on or off). 

4. Shell Script to create a msgbox using dialog tool : 

Let’s try to create a msgbox that displays a text message to users on a Linux Machine. 

Syntax : 

--msgbox <text> <height> <width> 

messg.sh : 

#!/bin/bash 
function DialogGen () {  
--msgbox "Hi geeks, hope everyone are enjoying GeeksForGeeks" 10 30 
} 
DialogGen 

Output : 

Understanding options used in the above Dialog Box : 

  • -msgbox : This keyword initiates the msgbox dialog Box on the shell console. 
  • -text: This text includes messages we want to convey to end-users.
  • -10 : this is the value set for Height. 
  • -30 : value set for width.

5. Shell Script to generate Gauge Dialog Box : 

Let us generate a shell script function that displays the Gauge type of Dialog Box on the shell console. 

Syntax : 

--gauge <text> <height> <width> [<percent>] 

messg.sh : 

#!/bin/bash 
function DialogGen 
{
--gauge "in progress..." 10 20 40 
}
DialogGen 

Output : 

6. Shell Script to generate Form Dialog Box : 

let us generate a shell script function that displays a form on the console shell. 

Syntax : 

--form <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> … 

messg.sh : 

function DialogGen () {
dialog --form "please enter the required information" 12 40 4 "Name:"  1 1 "" 1 12 15 0 \ "Age:"  2 1 "" 2 12 15 0 "Mail id:" 3 1 "" 3 12 15 0 
}
DialogGen 

Output : 

7. Shell Script to generate infobox Dialog Box on Linux Machine : 

In this, let us generate a Shell Script function that runs the infobox dialog box. 

Syntax : 

--infobox      <text> <height> <width>

messg.sh : 

#!/bin/bash 
function DialogGen () { 
    dialog --infobox "Geeks, this is just a sample message" 10 31 
}
DialogGen 

Output : 

8. Shell Script to generate inputbox dialog box on a Linux machine : 

In this, we try to generate a Shell Script function that runs an input box type dialog Box. 

Syntax : 

--inputbox     <text> <height> <width> [<init>]

messg.sh : 

#!/bin/bash  
function DialogGen() {
    dialog --inputbox "geeks, please enter your name" 10 31 "enter here"  
}
DialogGen 

Output : 

9. Shell Script to generate InputMenu Dialog Box

Now, let us write a shell script function to generate InputMenu Dialog Box on a Linux Machine.  

Syntax : 

--inputmenu    <text> <height> <width> <menu height> <tag1> <item1>… 

messg.sh : 

#!/bin/bash 
function DialogGen() {
    dialog --inputmenu "Edit if required," 12 45 25 1 "hi" 2 "good morning" 3 "Take" "Care"
} 
DialogGen 

Output : 

10. Shell Script to generate Menu from Dialog Box : 

In this script, we try to create a Shell Script Function that runs Menu Widget Dialog Box. 

Syntax

--menu         <text> <height> <width> <menu height> <tag1> <item1>…

messg.sh : 

#!/bin/bash 
function DialogGen() {
    dialog--menu "choose your favourite fruit" 12 45 25 1 "apple" 2 "banana" 3 "grapes" 4 "oranges" 
}
DialogGen 

Output : 

11. Shell Script to create Yes/No Box : 

In this Script, we generate a function that displays Dialog Box GUI with a yes or no option to users. 

Syntax : 

-yesno  <text> <height> <width>

Script : 

messg.sh : 

#!/bin/bash 
function DialogGen() {
    dialog --yesno "Hey Geek, Do You Want To Continue With Current Session?" 10 31 
}
DialogGen

Output : 

12. Shell Script to design a Password Box : 

In this script our job is to generate a function that displays, a password form with hidden Text fields. 

Syntax : 

--passwordform <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>… 

Script : 

messg.sh :

#!/bin/bash 
function DialogGen() { 
    dialog --passwordform "Hey Geek, please enter required information:" 12 40 4 "password:" 1 1 "" 1 12 15 0 "otp:" 2 1 "" 2 12 15 0 "secret key:" 3 1 "" 3 12 15 0 
} 
DialogGen 

Output : 

13. Shell Script to display Radio Box Dialogue : 

In this, let’s run a function in a shell script that generates Radio Box Dialogue on a Linux Machine :

Syntax :  

--radiolist    <text> <height> <width> <list height> <tag1> <item1> <status1>… 

Script : 

#/bin/bash 
function DialogGen() {
    dialog --radiolist 'radiolist' 15 10 10 'Grapes' 5 'off' 'apple' 2 'off' 'dessert' 3 'off' 'coffee' 4 'on' 
}
DialogGen 

Output : 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads