Getting to Know the Whiptail Dialogue Maker

botond published 2018. 10. 22., h - 02:44 time

The 1. page content

 

Introductory

You're Shell Bash When creating scripts, it is beneficial to spice up our work with visual elements that make our programs more usable, comfortable and spectacular. It is also important that our codes remain portable, so that they can be run on other computers. A whiptail program, which can be installed on any Linux distribution, so there is no obstacle to running our programs in other places where we created dialogs with the whiptail command.

In this tutorial, we get to know the whiptail command and make the basic dialog window instances. Whiptail is not new, but I haven't found a comprehensive description of how it works in Hungarian, so I hope this guide will be useful.

 

 

Installation

If you do not already have the whiptail package installed on your machine, you can easily do so by issuing the following installer as root. APT (for Debian distributions):

apt-get install whiptail

 

dialogues

The program offers several types of dialog boxes that are suitable for different tasks:

Info Box (Infobox)

The Info Box or Common Name info box a simple panel that does not hang the program run, just draws the window with the specified title and content and exits. Display of the program --infobox parameter. Syntax:

--infobox <szöveg> <magasság> <szélesség>
  • Text: Text displayed on the panel
  • Height: The height of the panel. Including frames.
  • Width: The width of the panel. Including frames.

Simple example:

whiptail --title "Példa infópanel címe" --infobox "Példa infópanel tartalma" 8 40

The result:

About Whiptail - Infobox

And here we can stop for a while, because if we type the command just now into an x ​​terminal, or even Putty, nothing will happen because the program is a known bug this feature does not work on xterms, but only on a normal console. So I do this one now VirtualBox on my machine lured it in console mode, which I could take a screenshot of.

So basically, the info box is good for displaying and then running the code so that we can do other things in the meantime, and when we write to the output, we get it out, and so on.

But since it only works on a console, let’s just look at it as interesting, avoid using it, and prefer to use, for example, the Message box-him.

Message box

The message box, in its original name Message box is used to put an Ok button at the bottom of the displayed text window, stopping the program run and waiting for the user to interact. For example, it can be used to display important information that the user must see before proceeding.

Using the -msgbox parameter:

--msgbox <szöveg> <magasság> <szélesség>
  • Text: Text displayed on the panel
  • Height: The height of the panel. Including frames.
  • Width: The width of the panel. Including frames.

Simple example:

whiptail --title "Példa üzenetdoboz" --msgbox "Ez az üzenetdoboz tartalma. Ok-ra nyomva mehet tovább..." 8 60

The whiptail command has one more backtitle also an option to give the entire window or screen a title that appears in the upper left corner. If you already have one, do this:

whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa üzenetdoboz" \
    --msgbox "Ez az üzenetdoboz tartalma. Ok-ra nyomva mehet tovább..." \
    8 60

The output of this is:

Whiptail - Message box introduction

As you can see in the picture, this is already displayed in a PuTTY window, so from now on the functions are compatible with all terminals.

There is already interaction with this type of window so the user has the choice of either pressing the Ok button or the ESC key. In both cases, the whiptail exits, but it generates other error states that $? variable returns. If you press the Ok key, the error code is = 0, and if you exit with the ESC key, you get an 255 code. This way, we can evaluate the exit status in our program and continue running accordingly.

A simple example is:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa üzenetdoboz" \
    --msgbox "Ez az üzenetdoboz tartalma. Ok-ra nyomva mehet tovább..." \
    8 60
    
if [[ $? == 0 ]] ; then
    echo "Kilépés Ok gombbal."
else
    echo "Kilépés ESC billentyűvel"
fi

Yes / no question box (Yes / no box)

The point of this type of dialog is that you can ask the user questions that you can answer yes or no, but here too you have the option to exit with the ESC key.

The question box is a --yes parameter:

--yesno  <szöveg> <magasság> <szélesség>
  • Text: Text displayed on the panel
  • Height: The height of the panel. Including frames.
  • Width: The width of the panel. Including frames.

In the following example we ask the question with the questioner box and then evaluate the user's response with a case branch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa kérdés doboz" \
    --yesno "Igen/nem választási példa. Melyiket választja?" \
    8 60
 
# Kiértékelés
case $? in
0)
    echo "Kilépés az Igen gombbal"
;;
1)
    echo "Kilépés a Nem gombbal"
;;
255)
    echo "Kilépés az ESC billentyűvel"
;;
*)
    # Ez nem fordulhat elő, csak betettem, hogy szabályosak legyünk...
    echo "Érvénytelen hibakód."
;;
esac

Whiptail - Yes / No box introduction

It is also worthwhile to use an external loop to "return" the user to the question again if the ESC key has been pressed. I also put in an embedded message window informing the user of the invalid (ESC) choice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
while true; do      # Végtelen ciklus indul
    whiptail \
        --backtitle "Ablak főcíme" \
        --title "Példa kérdés doboz" \
        --yesno "Igen/nem választási példa. Melyiket választja?" \
        8 60
 
    # Kiértékelés
    case $? in
    0)
        echo "Érvényes választás: Igen"
        break       # kilépés a végtelen ciklusból
    ;;
    1)
        echo "Érvényes választás: Nem"
        break       # kilépés a végtelen ciklusból
    ;;
    255)            # Ha ESC gombot nyomtak, akkor
        # Beágyazott whiptail hibaüzenet ablak, amiben kiírjuk, hogy
        # érvénytelen volt a választás, és ezt még le kell okéznia
        whiptail \
            --backtitle "Hiba!" \
            --title "Érvénytelen választás!" \
            --msgbox "Kérem adja meg valamelyik érvényes választ!" \
            8 60
        # És itt nem lépünk ki a ciklusból hogy újra lefuttassa az egészet
    ;;
    *)
        # Ez nem fordulhat elő, csak betettem, hogy szabályosak legyünk...
        echo "Érvénytelen hibakód."
    ;;
    esac
done

I have spelled it out a bit, but this way we can be sure that pressing the ESC key will not cause a malfunction in our program and we will also inform the user about the use of invalid ESC.

 

 

Text input field (Input box)

The text input field allows us to request a text response from the user. THE --inputbox parameter:

--inputbox <szöveg> <magasság> <szélesség> [alapszöveg]
  • Text: Text displayed on the panel
  • Height: The height of the panel. Including frames.
  • Width: The width of the panel. Including frames.
  • Basic Text: Here you can enter default text as the last value of the inputbox parameter, which makes the text request panel even more functional.

Simple example:

1
2
3
4
5
#!/bin/bash
whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa üzenetdoboz" \
    --inputbox "Kérdés .... ?" 8 40 "alapértelmezett szöveg"

Whiptail - Input box introduction

Here, pressing the Ok key will exit and the entered text will be displayed. If you press the Cancel key or the ESC key, the text in the box will not be displayed.

In the usual way for the previous panels, we return the value corresponding to the key pressed in the error code ($?) Variable: Ok key: 0, Cancel key: 1 and the ESC key can be read the value 255 after the run. But what about the typed text?

If we want to capture the typed text into a variable, we have to do a bit of trick because whiptail writes the string in the field to standard error output (stderr). Use the standard outputs and inputs (stdout, stdin) to display the window and request characters, leaving only the error output to transmit the entered value.

However, only the standard outputs (stdout) of commands run in subshells are issued to the external shell, so when running the dialog, the output must be replaced with the error output so that we can extract the requested text. With an abbreviated example, this looks like this:

valasz=$(whiptail --inputbox "Kérdés?" 8 40 "Alap" --title "Ablakcím" 3>&1 1>&2 2>&3)
echo $valasz

So this is the way to read the text you type into the variable from the Input Box.

Alternatively, it is possible to save the user-entered response directly to a file, for example for later processing:

whiptail --inputbox "Kérdés?" 8 40 "Alap" --title "Ablakcím" 2>tmpfile

Again, the error output (stderr) is redirected to any file.

Now I'm not complicating the evaluation of the buttons and the handling of the ESC key.

Text box

The text box allows you to display the contents of text files in a dialog box. It is practical to use, as the text content is separate from the program code, so it is easier to modify it from the outside, and we do not increase the size of the program code with text content and our code remains more readable.

Using a --textbox parameter:

--textbox <fájlnév> <magasság> <szélesség>
  • Filename: The text file to be scanned
  • Height: The height of the panel. Including frames.
  • Width: The width of the panel. Including frames.

To test it, create a new text file eg. the dwarf editor:

nano textbox_file

And let's say say a few sentences of Lorem Ipsum:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Then make our Bash program that displays the dialog:

1
2
3
4
5
#!/bin/bash
whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa szövegdoboz" \
    --textbox ./textbox_file 16 60

About Whiptail - Textbox

Here again, the error code variable ($?) Can be read out and the program can continue to run according to its value (Ok = 0, ESC = 255), so for example, pressing the ESC key can stop the whole program, etc.

Also, it's a good idea to take advantage of the whiptail scrolltext option here, which allows you to display longer text contents in a scrollable window.

First, double the text in the text file for a more illustrative example:

echo $(cat ./textbox_file) >> ./textbox_file

And with the scrolltext switch added:

1
2
3
4
5
6
#!/bin/bash
whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa szövegdoboz" \
    --textbox ./textbox_file 16 60 \
    --scrolltext

Whiptail - Introducing a scrollable textbox

Although the scroll element itself does not want to appear right now (I have tried it on several machines now), it can still scroll the text nicely with the cursor keys. It may be the problem with my version that only one outage appears on the right side of the dialog box frame, but it appeared earlier in a different situation. The point is, scrolling works anyway.

 

Navigation

This description consists of several pages: