Getting to Know the Whiptail Dialog Maker (page 2)

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

2. page content

 

Dialogues (Continued from Page 1)

Az on the first page we got to know some types of Whiptail dialogs, here we continue to review the remaining window types.

Password box

The password prompt box allows you to safely retrieve passwords, as typed text is replaced by asterisks, as is customary with passwords. This way, we can conveniently and elegantly solve an access control panel at the beginning of our programs to keep unauthorized users away.

The panel a --passwordbox can be invoked with a switch whose syntax is:

 --passwordbox <bekérő szöveg> <magasság> <szélesség> [kezdőérték]
  • Requested text: Text to write above the password field
  • Height: Panel height (including frames).
  • Width: Panel width (including frames).
  • Starting Value: Optional, you can also enter a start value that is set when the panel is displayed.
    However, it can confuse the user, so avoid using it.

Basic example:

1
2
3
4
5
6
#!/bin/bash
whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa jelszó bekérő doboz" \
    --passwordbox "Kérem adja meg a jelszót" \
    8 60

And the appearance of the panel:

Whiptail - Password box introduction

In terms of buttons, this kind of dialog is similar to the previous one Yes / No to the question box, so it is advisable to work out the button handling here as well.

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 jelszó bekérő doboz" \
    --passwordbox "Kérem adja meg a jelszót" \
    8 60
 
# Kiértékelés
case $? in
    0)
        echo "Gomb használat: Ok gomb"
    ;;
    1)
        echo "Gomb használat: Mégsem gomb"
    ;;
    255)
        echo "Gomb használat: ESC billentyű"
    ;;
    *)
        # Ez nem fordulhat elő, csak betettem, hogy szabályosak legyünk...
        echo "Érvénytelen hibakód."
    ;;
esac

In terms of value, it is Input boxis similar to the standard error output (stderr), so you can count on the password you entered.

Accordingly, if you want to load the typed password into a variable, you also need to swap the outputs here when you run the whiptail command in the subshell:

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
#!/bin/bash
 
jelszo=$( \
    whiptail \
        --backtitle "Ablak főcíme" \
        --title "Példa jelszó bekérő doboz" \
        --passwordbox "Kérem adja meg a jelszót" \
        8 60 \
        3>&1 1>&2 2>&3)
 
# Kiértékelés
case $? in
    0)
        echo "Gomb használat: Ok gomb"
        echo "A megadott jelszó: $jelszo"
    ;;
    1)
        echo "Gomb használat: Mégsem gomb"
    ;;
    255)
        echo "Gomb használat: ESC billentyű"
    ;;
    *)
        # Ez nem fordulhat elő, csak betettem, hogy szabályosak legyünk...
        echo "Érvénytelen hibakód."
    ;;
esac

In this way, the received password is nicely included in the variable, which is used only when the Ok button is pressed. And this can be further complicated from here, from password verification to various filtering, and so on.

 

 

With the menu dialog we can create classic light strip menus. Operating a --menu with the following syntax:

--menu <menüszöveg> <ablak magassága> <ablak szélessége> <lista magassága> <menüelem> <leírás> ...
  • Menu text: Text displayed above menu options
  • Window height: The usual window height.
  • Window width: The usual window width.
  • List height: Here you can control how many menu items are displayed at a time. This should be adjusted to the height of the window so that the menu does not hang out of the window.
  • Menu item: The value of the menu item displayed (left column). This is output to the standard error output, so we will get this in the variable as well. It can be text or just a one-digit menu item.
  • Description: This is a description of the menu item. This is only needed for the text displayed in the menu.

The last two (menu items and descriptions) must be repeated in spaces separated by spaces, eg: "1" "Menu1" "2" "Menu2" "3" "Menu3" ... Etc. You can insert as many menu items as you need.

And our example program, which, like a password rescuer, already loads results and evaluates keystrokes:

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
35
36
37
#!/bin/bash
 
menu=$( \
    whiptail \
        --backtitle "Ablak főcíme" \
        --title "Példa menü" \
        --menu "Kérem válassza ki a megfelelő opciót" 14 60 6 \
            "Első menüpont" "Első menüpont leírása" \
            "Második menüpont" "Második menüpont leírása" \
            "Harmadik menüpont" "Harmadik menüpont leírása" \
            "Negyedik menüpont" "Negyedik menüpont leírása" \
            "Ötödik menüpont" "Ötödik menüpont leírása" \
            "Hatodik menüpont" "Hatodik menüpont leírása" \
            "Hetedik menüpont" "Hetedik menüpont leírása" \
            "Nyolcadik menüpont" "Nyolcadik menüpont leírása" \
            "Kilencedik menüpont" "Kilencedik menüpont leírása" \
            "Tizedik menüpont" "Tizedik  menüpont leírása" \
        3>&1 1>&2 2>&3)
    
        
# Kiértékelés
case $? in
    0)
        echo "Gomb használat: Ok gomb"
        echo "A kiválasztott menüelem: $menu"
    ;;
    1)
        echo "Gomb használat: Mégsem gomb"
    ;;
    255)
        echo "Gomb használat: ESC billentyű"
    ;;
    *)
        # Ez nem fordulhat elő, csak betettem, hogy szabályosak legyünk...
        echo "Érvénytelen hibakód."
    ;;
esac

Whiptail - Menu Box Introduction

Interestingly, a small scroll bar appears here, which is intended to show the position of the current menu item relative to the entire list if the menu items do not fit on the panel at the same time.

Pressing the Ok button returns the value field for the selected menu item, which is the one in the left column in this example. Here we can use numbering instead of names, and then we need to process numeric values ​​in the rest of the program, and there is more space for menu descriptions in the right column. Buttons are handled in the same way as in the previous examples (Ok / Cancel / ESC buttons).

Check list

With the option lists (this is probably the most appropriate name for it) we can, for example, create various configuration panels that allow you to turn the options on and off independently.

This panel is a --checklist parameter can be invoked with the following syntax:

 --checklist <szöveg> <magasság> <szélesség> <lista-magasság> [érték címke állapot] ...
  • Text: Text to display above list
  • Height: Height of the standard window
  • Width: The width of the standard window
  • List Height: Again, you can control how many options are displayed at a time. If there are more, the scroll function appears.
  • Value label status: Here, we need to specify the properties of the options in triples:
    - Value: The value of the option. This is transmitted in the program
    - label: Option label, anything you can type here
    - Status: This is the default status of the option. For example, in a configuration panel, these are used to populate the status of settings already saved.

Example Program:

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
selected_options=$( \
    whiptail \
        --backtitle "Ablak főcíme" \
        --title "Példa checklist" \
        --checklist \
            "Kérem válassza ki a megfelelő beállításokat" \
            14 60 6 \
            "A opció" "A opció leírása" ON \
            "B opció" "B opció leírása" OFF \
            "C opció" "C opció leírása" ON \
            "1" "1-es opció leírása" OFF \
            "2" "2-es opció leírása" ON \
            "3" "3-as opció leírása" OFF \
        --separate-output \
        3>&1 1>&2 2>&3)
 
# Kiértékelés
case $? in
    0)
        echo "Gomb használat: Ok gomb"
        echo "A kiválasztott opciók: $selected_options"
    ;;
    1)
        echo "Gomb használat: Mégsem gomb"
    ;;
    255)
        echo "Gomb használat: ESC billentyű"
    ;;
    *)
        # Ez nem fordulhat elő, csak betettem, hogy szabályosak legyünk...
        echo "Érvénytelen hibakód."
    ;;
esac

Introducing Whiptail - Checklist

This panel works just like the menu, so there is nothing to hang on it. The only difference is that here you can get more options depending on the options you have set in the panel. In such cases, a --separate-output switch as well, as this will produce an output that is easier to process. Without it, the selected options are enclosed in quotation marks, which is macerated.

Radio buttons

You can use the radio buttons to create multi-state switches where only one item can be set at a time.

The panel a --radiolist parameter with the following syntax:

--radiolist <szöveg> <magasság> <szélesség> <lista-magasság> [érték címke állapot] ...

Its use is the same as in the previous option list dialog.

Example:

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
#!/bin/bash
selected_option=$( \
    whiptail \
        --backtitle "Ablak főcíme" \
        --title "Példa rádió gombok" \
        --radiolist \
            "Kérem állítsa be a megfelelő értéket" \
            14 60 6 \
            "1" "1-es beállítás" OFF \
            "2" "2-es beállítás" ON \
            "3" "3-as beállítás" OFF \
        3>&1 1>&2 2>&3)
 
# Kiértékelés
case $? in
    0)
        echo "Gomb használat: Ok gomb"
        echo "A kiválasztott beállítás: $selected_option"
    ;;
    1)
        echo "Gomb használat: Mégsem gomb"
    ;;
    255)
        echo "Gomb használat: ESC billentyű"
    ;;
    *)
        # Ez nem fordulhat elő, csak betettem, hogy szabályosak legyünk...
        echo "Érvénytelen hibakód."
    ;;
esac

Whiptail - Introduction of radio buttons

As before, the button status and the selected option should be read in the same way. Here we get a single value at the standard error output, so the result can be easily processed.

 

 

Progress gauge

With the progress bar, you can "hit" the time while, for example, a longer process is running in the background. This allows the user to see where the program is running while waiting.

The panel a --gauge parameter can be called:

--gauge <szöveg> <magasság> <szélesség> <százalék>
  • Text: Standard panel text
  • Height: Panel height
  • Width: Panel width
  • Percentage: Sets the progress bar to this percentage

A simple static example:

1
2
3
4
5
#!/bin/bash
whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa szövegdoboz" \
    --gauge "A folyamat állapota:" 8 65 12

Whiptail - Introducing a progress bar

If you start the program this way, it will "get stuck" and you will not be able to exit it, close max terminal or exit from another terminal. It does not hang the program, as this is to work in the background, it only converts the output to this image. Therefore, you should use this panel by moving the percentages from one cycle to do our longer background tasks, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
{
    for ((i = 0 ; i <= 100 ; i+=1)); do
        # háttér feladatok elvégzése
        sleep 0.1       # kis késleltetés
        # ...
        # ...
        
        # százalék kiírása a kimenetre, amit csővezetéken megkap a whiptail
        echo $i
    done
} | whiptail \
    --backtitle "Ablak főcíme" \
    --title "Példa szövegdoboz" \
    --gauge "A folyamat állapota:" 8 65 0

In this example, the above cycle "drives" the progress indicator. In the cycle, write only the percentage state to the output that the whiptail receives continuously through a pipeline. So it starts from the initial setting 0% and goes up to 100% and exits. And in the cycle, we can do the background tasks, which of course we have broken down into percentages. Pl files copied bit by bit, etc.

Of course, it can also be driven by other types of cycles or, for example, large-scale file readings or other scales, the point is to continuously pass the percentage value to the whiptail at the output.

 

Conclusion

In this two-sided description, we learned about the possibilities provided by the whiptail command, which allows us to display various dialog boxes quickly and with little program code. When writing larger programs, it is especially useful to be able to organize parts of the project into menus with the appropriate panels, or to provide them with various configuration panels and save options.

 

 

Navigation

This description consists of several pages: