Dialog: Using Dialog boxes to interact with users on a Linux console.

You can’t throw in a GUI for an application that will be run on an enviornment without X. Does that mean you’re left to live with bland command-line prompts for interaction? Think again.

Ever done a text-based install of a Linux distribution? If yes, then I bet you haven’t missed out on those colourful boxes, pitched in combinations of red and blue or yellow and blue, requesting you to type in input and displaying output in return. If you have ever installed Slackware, any of the BSDs, or Debian, I am sure you can’t have overlooked those boxes. What are those? Don’t they look cool? Wouldn’t you want to have one to handle interactions with the enviornment on your command-line application(s)? If you’re anything like me who writes only applications which run from the console, sure as hell you do. I’ve got one word for you: dialog.

Yes! Dialog! Dialog is a program you can find on almost any Linux distribution. It is a program that displays colourful dialog boxes for interaction with the user. It can display information boxes, which contain nothing but text to display on the screen inside a colourful box, menu bars, input boxes, check boxes, etc. You name it, dialog has it.

The manpage documents dialog(1) in detail, being the all-in-one manual to understand how to use dialog and start using it. I see no point in re-hashing the contents of the manpage in my own words. But, yes, I think a simple BASH script to make use of the dialog utility to create a couple of dialogs to take input from user would be sufficient to get the curious onlooker started.

#!/bin/bash

dialog --backtitle "Dialog Test Example" --title "Network Settings"
--no-shadow --menu "DHCP or Manual" 0 0 0 "dhcp" "Enable DHCP"
"manual" "Manual Ip Setting" 2> /tmp/output.file

dialog --backtitle "Dialog Test Example" --title "IP Settings"
--no-shadow --inputbox "EnterIP:" 0 0 "192.168.1.10" 2> /tmp/output.file

# End

Execute the script and be mesmerised by the magic of dialog. By the way, do take a look at the file /tmp/output.file when you’re done running this simple script. ;-)

6 Comments

  1. Posted 15 August, 2006 at 8:51 am | Permalink

    heh nice one, i always thought they did it with programmatically with curses

  2. Posted 15 August, 2006 at 9:07 am | Permalink

    Heh, yea. I used to think so too, until, one boring day, while editing a couple of maketag scripts found on Slackware CDs, I bumped across “dialog”. I’d like to think I stumbled over a treasure chest. ;-)

  3. nthnth
    Posted 21 October, 2006 at 10:03 am | Permalink

    Dialog is a stultifyingly stupid piece of shit, adding no further functionality to a choice-driven script than colors.

    But hey, if you like colors, it is soooooo cooooool

  4. Nabeel
    Posted 25 January, 2007 at 8:10 pm | Permalink

    nice one… I didnt know that either… definitely saw the dialog call in scripts but ignored it…

    nice one Ayaz

  5. Rehan
    Posted 22 July, 2007 at 4:49 pm | Permalink

    If you write your scripts to use dialog they can easily be ported to X by replacing the dialog command with the dialogx command (also found in many distro’s). You can also easily determine if you are in a terminal (use dialog) or if you are in X (use dialogX).

    Ideal for simple utility type applications (and very fast).

  6. Posted 23 July, 2007 at 4:37 am | Permalink

    Thanks, Rehan. I didn’t know dialogX existed. I don’t have it on my Slackware system, though. I will look for it on the CentOS box I use at work.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*