Dialog: How to create menus in your scripts

dialog, mensajebox, tittle, backtittle, script, title, bash, debian, ubuntu

Dialog is a command of GNU / Linux that allows you to create dialog boxes in the terminal for you to use in your scripts programming, I'll show you how to use this command.

For the realization of this article I command basadoen the manual and on the following websites give my sincere thanks to the creators.

http://ovtoaster.com/scripts-en-linux-con-estilo/
http://linuxgazette.net/101/sunil.html

The colors in applications and systems

written texts in the command line: Blue
Output in the command line: Verde
File names and file content: brown

Installing dialog

To use this tool in linux first thing you have to do is install

Ubuntu

sudo apt-get install dialog

Debian

apt-get install dialog

Red hat

yum install dialog

Suse

zypper install dialog

Syntax

For boxes with only one option.

So much <width-of-box> <top-of-box> is represented by numerical values ​​if we 0 They conform to content.

dialog <options-common> <box options> <width-of-box> <top-of-box>

Example:

dialog --title "Title"  --yesno "You like the Red color?" 0 0

For cases with multiple options

It is only for type boxes checklist, menu or radiolist. If we 0 in <number-of-options-visible> it automatically adjusts to the number of options.

dialog <options-common> <box options> <width-of-box> <top-of-box> <number-of-options-visible>.

Example:

dialog --checklist "Choose the options you want:" 0 0 0  1 cheese on 2 "Mustard" on  3 anchovies off

common options

They are options that are present in most types of dialog boxes “dialog” the most common are the following:

 

Titles

dialog, mensajebox, tittle, backtittle, script, title, bash, debian, ubuntu

–title

It is used to put the dialog title.

–backtitle

It is the title of the window background.

Change text buttons

–yes-label

Change the word YES in the dialog boxes for your wish.

–ok–label

Change the word OK in the dialog boxes for your wish.

–cancel-label

Change the word CANCEL of the dialog boxes for your wish.

–exit-label

Change the word EXIT of the dialog boxes for your wish.

–no-label

Change the word NO in the dialog boxes for your wish.

–nook

Remove the OK button of the dialog box you need to press enter to enter.

Command Output

These variables allow you to decide where will the choices made in the command will by default stderr if we can get him to screen.

–stderr

Command output to stderr if we choose this option can get the output to a file by adding at the end of the line of dialog:

2><where to save the output file>

–stdout

With this option we will display the command output I use when I want to store in a variable command but do not want to be written to a temporary file, later in the examples we will see how.

 

Options box

Message Box (–msgbox)

dialog, mensajebox, tittle, backtittle, script, title, bash, debian, ubuntu

It serves to put a message on the screen until the user press enter.

dialog --title "Title" --msgbox "Hello World" 0 0

Menu YES / NO (–yesno)

dialog, yesno, script, bash, debian, ubuntu

It serves to answer questions with answer YES / NO if the answer is YES returns 0 If the answer is NO returns 1

dialog --title "Title"  --yesno "You like the Red color?" 0 0

Infobox (–infobox)

dialog, infobox, script, title, bash, debian, ubuntu

This box displays information we want is desirable to add a sleep (waiting x seconds to continue command) to view it.

dialog --infobox "Wait 4 seconds" 0 0 ; sleep 4

Headbox (–inputbox)

dialog, inputbox, script, title, bash, debian, ubuntu

It is used to collect data.

 dialog --title "name" --inputbox "Put your name:" 0 0

Password box (–PasswordBox)

dialog, PasswordBox, script, bash, debian, ubuntu

It is equal to the input box but the characters typed are not displayed.

 dialog --title "Password" --PasswordBox "Put your password:" 0 0

Box (–textbox)

dialog, tesxtbox, script, bash, debian, ubuntu, linux, GNU/linux

The box is a file viewer and shows us the file you indicate.

dialog --title "fstab file" --textbox /etc/fstab 0 0

Box menu (–menu)

dialog, menu, script, bash, debian, ubuntu, linux, GNU/linux, menu box

It allows us to choose an option among several numbered options

dialog --menu "The best tortilla is:" 0 0 0 1 "with onions" 2 "without onion" 3 "with piminetos" 

Box checklist (–checklist)

dialog, checklist, script, bash, debian, ubuntu, linux, GNU/linux

It is a menu with several options that we can choose several. The options put in on They will be lit and to put in off deleted.

dialog --checklist "Choose the options you want:" 0 0 0  1 cheese on 2 "Mustard" on  3 anchovies off

A special mention within checklist for option –separate-output that returns us a line for each selection made very useful for running different commands for each selection made.

Box radiolist (–radiolist)

dialog, radiolist, script, bash, debian, ubuntu, linux, GNU/linux

It is a menu with several options that we can choose only one.

dialog --radiolist "Choose your favorite omelette:" 0 0 0 1 "with onion" off  2 "Without onion" on 3 "peppers" off 4 "asparagus" off

Calendar box (–calendar)

dialog, calendar, calendar, script, bash, debian, ubuntu, linux, GNU/linux

It is a calendar where we can choose a date.

dialog --title "Calendar" --calendar "Choose an arrow" 0 0

Other options box

In addition to the options box that we have seen there are other options that might be interesting to use to know them do not hesitate to come to the aid.

man dialog

How to use the data obtained with dialog

To use a script to data compiled by the dialog boxes there varaias options dialog I've found, but surely occur to you it will not hesitate to comunicarmelas more and add in the post.

Redirect output to a file dialog.

We can store the output of command to a file dialog, usually we will put in the / tmp and delete after use, then I put a script with an example explained.

#!/bin/bash
# By Ignacio Alba Obaya
# http://aplicacionesysistemas.com
# run the dialog box by ending 2>/tmp / nombre.tmp. $$ 
# store it in a file name entered.
# remember that 2> redirects the error output to a file.
dialog --title "name" --inputbox "Put your name:" 0 0 2>/tmp / nombre.tmp. $$
# delete the screen
clear
# show the stored name
cat /tmp/nombre.tmp.$$
# delete the file named
rm -f /tmp/nombre.tmp.$$
# We make a line break so that we are not amass the prompt
echo -e "\n"

Store the answer in a variable.

To store the result we will do this by creating a function and calling it from a variable.

#!/bin/bash
# By Ignacio Alba Obaya
# http://aplicacionesysistemas.com
# Store the result in a variable dialog
# We create the function fundialog
fundialog = ${fundialog=dialog}
# We create a variable with the output date dialog 
# redireccinando with output dialog --stdout 
# to standard output, Note that the function is between 
# accents of the key [ if not it does not work.
fecha=`$fundialog --stdout --title "Calendar" --calendar "Choose a date" 0 0`
# Show captured date
echo $ date

How to manage outputs checklist

We also need to manage multiple output options priate have a checklist so that each option trigger different events. A hobble to do this is as follows:

#!/bin/bash
# By Ignacio Alba Obaya
# http://aplicacionesysistemas.com

# We created the varaible funcheck in which we store 
# order dialog with the option --separate-output
funcheck =(dialog --separate-output --checklist "Select the groups they belong:" 0 0 0)

# We define the options on the screen
# appear lit the we have put on.
options =(1 "option 1" on 
 2 "option 2" off
 3 "option 3" off
 4 "option 4" off
 5 "option 5" on
 6 "option 6" off
 7 "option 7" off)

# We create the function selections with options running funcheck 
# and forwards the output to the terminal for the next run
# the commands
selections = $("${funcheck[@]}" "${options[@]}" 2>&1 >/dev/tty)

# clean the screen
clear

# add a for loop to run a command function 
# the selections can change the echo by 
# any commands or scripts
for selection in $ selections
do
 $ selection in case
 1)
 echo "You chose the option 1"
 ;;
 2)
 echo "You chose the option 2"
 ;;
 3)
 echo "You chose the option 3"
 ;;
 4)
 echo "You chose the option 4"
 ;;
 5)
 echo "You chose the option 5"
 ;;
 6)
 echo "You chose the option 6"
 ;;
 7)
 echo "You chose the option 7"
 ;;
 esac
done

Conclusions on dialog

In my opinion dialog is an elegant way to create menus for our scripts can easily create programs for terminal.

Thank you for reading our blog hope you like our content, I would ask you not to forget +1 or share on social networks, no ganamos nada con este blog y es para nosotros cada me gusta o mas 1 es una pequeña recompensa 😀


About Ignacio Alba Obaya

Microcomputer and Technical Training for Employment. Lover of new technologies. Manager GNU / Linux and Windows.

Leave a Reply