Table of Contents

Dialog Boxes


There are multiple dialog boxes tools. Choice mainly depend on features you need. Very light, go with Whiptail. More advanced (file explorer, date/time, file editor, forms, etc), but still simple, go with Dialog. Advanced (like GUI with multiple windows, menu, etc), go with Newt.

Whiptail

Whiptail is used directly from bash.

Example is better than words sometime:

while true; do
 
  choice=$(whiptail --title "Choose dialog to open" --menu "Please choose a dialog to open" 0 0 0 \
  "1" "inputbox" \
  "2" "yesno" \
  "3" "msgbox" \
  "4" "passwordbox" \
  "5" "gauge" \
  "6" "textbox" \
  "7" "exit" 3>&1 1>&2 2>&3)
 
  case "$choice" in
    1)
       inputboxresult=$(whiptail --inputbox "This is an inputbox" --title "inputbox" 0 0 3>&1 1>&2 2>&3)
       echo $inputboxresult
       ;;
    2)
       whiptail  --yesno "This is a yesno box" --title "yesno" 0 0
       echo $?
       ;;
    3)
       whiptail  --msgbox "This is a msgbox" --title "msgbox" 0 0
       echo $?
       ;;
    4)
       passwordboxresult=$(whiptail --passwordbox "This is a passwordbox" --title "passwordbox" 0 0 3>&1 1>&2 2>&3)
       echo $passwordboxresult
       ;;
    5)
       (
cat <<EOF
XXX
10
This is step one...
XXX
EOF
touch myfile > /dev/null
sleep 1s
cat <<EOF
XXX
70
This is step two...
XXX
EOF
touch myfile > /dev/null
sleep 1s
cat <<EOF
XXX
100
This is step three...
XXX
EOF
sleep 1
) | whiptail --gauge "Please wait..." 6 50 0
       ;;
    6)
       echo "hello" > /tmp/myfile
       echo "world" >> /tmp/myfile
       echo "!" >> /tmp/myfile
       whiptail --textbox --scrolltext /tmp/myfile 20 0
       echo $?
       ;;
    7)
       break
       ;;
  esac
 
done

Good documentation:

https://en.wikibooks.org/wiki/Bash_Shell_Scripting/Whiptail

Dialog

Dialog is used directly from bash. On most distributions, you need to install dialog (yum install dialog). Dialog syntax is very close to Whiptail.

Example is better than words sometime:

while true; do
 
  choice=$(dialog --backtitle "My dialog example" --title "Choose dialog to open" --menu "Please choose a dialog to open" 0 50 0 \
  "1" "inputbox" \
  "2" "yesno" \
  "3" "msgbox" \
  "4" "passwordbox" \
  "5" "gauge" \
  "6" "textbox" \
  "7" "exit" 3>&1 1>&2 2>&3)
 
  case "$choice" in
    1)
       inputboxresult=$(dialog --backtitle "My dialog example" --title "inputbox" --inputbox "This is an inputbox" 8 50 3>&1 1>&2 2>&3)
       echo $inputboxresult
       ;;
    2)
       dialog --ascii-lines --backtitle "My dialog example" --title "yesno" --yesno "This is a yesno box" 6 50
       echo $?
       ;;
    3)
       dialog --colors --backtitle "My dialog example" --title "msgbox" --msgbox "This \Zb\Z1is a msg\Znbox" 6 50
       echo $?
       ;;
    4)
       passwordboxresult=$(dialog --backtitle "My dialog example" --title "passwordbox" --passwordbox "This is a passwordbox" 8 50 3>&1 1>&2 2>&3)
       echo $passwordboxresult
       ;;
    5)
       (
cat <<EOF
XXX
10
This is step one...
XXX
EOF
touch myfile > /dev/null
sleep 1s
cat <<EOF
XXX
70
This is step two...
XXX
EOF
touch myfile > /dev/null
sleep 1s
cat <<EOF
XXX
100
This is step three...
XXX
EOF
sleep 1
) | dialog --backtitle "My dialog example" --gauge "Please wait..." 7 50 0
       ;;
    6)
       echo "hello" > /tmp/myfile
       echo "world" >> /tmp/myfile
       echo "!" >> /tmp/myfile
       dialog --backtitle "My dialog example" --textbox /tmp/myfile 20 0
       echo $?
       ;;
    7)
       break
       ;;
  esac
 
done

Note: if you get strange characters instead of lines and you are using putty on windows, use –ascci-lines for each dialog command, or before opening ssh, go into putty configuration, then in Connection → Data, change Terminalt-type string from xterm to linux. Ensure also that in Window → Translation, UTF-8 is used.

Good documentation:

http://www.unixcl.com/2009/12/linux-dialog-utility-short-tutorial.html

Newt

Newt is based on C language, but is very powerfull.

First install some packages (here on centos):

yum install newt newt-devel slang-devel

Then an ugly example would be:

#include <newt.h>
#include <stdlib.h>
#include <stdio.h>
 
int main(void) {
 
void * result;
    struct partition * curr;
    newtInit();
    newtCls();
 
    newtDrawRootText(0, 0, "My manager");
    newtDrawRootText(4, 4, "This is a test manager");
    newtDrawRootText(4, 6, "All actions done here are logged in /var/log/manager.log");
 
    newtComponent win1, form, b1, b2, listbox;
    win1 = newtOpenWindow(10, 10, 80, 30, "Button Sample");
    b1 = newtButton(10, 9, "Ok");
    b2 = newtButton(22, 9, "Cancel");
    listbox= newtListbox(10,15,2, NEWT_FLAG_RETURNEXIT);
    newtListboxAppendEntry(listbox, "do nothing" , "1");
    newtListboxAppendEntry(listbox, "touch file" , "2");
    form = newtForm(NULL, NULL, 0);
    newtFormAddComponents(form, b1, b2, listbox, NULL);
    newtRunForm(form);
    result = newtListboxGetCurrent(listbox);
 
 
    newtFormDestroy(form);
    newtPopWindow();
 
    newtFinished();
    const char * toto=result;
    printf("%c\n",*toto);
    printf("%p\n",result);
    if ( toto == "2" )
    {int status = system("touch /tmp/testr");}
}

To build it:

gcc -o test.out test.c -lnewt

Full documentation can be found at: http://gnewt.sourceforge.net/tutorial.html
To get more informations on functions: https://sourcecodebrowser.com/newt/0.52.10/listbox_8c.html