This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
documentation:shelldialog [2010/12/18 02:12] – mgough | documentation:shelldialog [2011/11/11 00:02] (current) – [Objective] haas | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Shell Interfaces with Dialog and Whiptail====== | ||
+ | =====Objective===== | ||
+ | To learn and understand the dialog and whiptail commands to use them in shell scripts for fun and profit. | ||
+ | |||
+ | =====Background===== | ||
+ | Once upon a time there was a program called dialog which was used to display text-based widgets in shell scripts. Over time, this software evolved and expanded, seeing code, license, and feature changes. | ||
+ | |||
+ | At some point, Whiptail emerged onto the scene. Whiptail is a dialog-like clone which aims to be lighter weight and makes use of the newt library (vs. the ncurses library which dialog uses). | ||
+ | |||
+ | =====Usage===== | ||
+ | |||
+ | ====Dialog Screenshots==== | ||
+ | It's easy to think that the shell interlace is boring and text-y, but you are wrong! | ||
+ | |||
+ | We've all made menus for programs made in shell, and as hard as you try this is about as fancy as you can get. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | With one easy command, you can have menus like this! | ||
+ | |||
+ | {{: | ||
+ | |||
+ | This is actually much easier than you might think - though it will take a bit more code in your shell script to make it functional. | ||
+ | |||
+ | ====Creating a Menu==== | ||
+ | To draw this menu, the command line that I used was: | ||
+ | <cli> | ||
+ | lab46:~$ dialog --menu "Fancy Menu 2" 50 50 100 " | ||
+ | </ | ||
+ | |||
+ | Pretty simple right? By typing " | ||
+ | < | ||
+ | --menu | ||
+ | </ | ||
+ | |||
+ | Taking a look at the menu implementation you can see that we are presented the options that the command will take as an argument. | ||
+ | |||
+ | < | ||
+ | |||
+ | < | ||
+ | |||
+ | < | ||
+ | |||
+ | < | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | ====Other Types of Dialogs==== | ||
+ | We are not just limited to menu applications. | ||
+ | < | ||
+ | --calendar | ||
+ | --checklist | ||
+ | --dselect | ||
+ | --editbox | ||
+ | --form | ||
+ | --fselect | ||
+ | --gauge | ||
+ | --infobox | ||
+ | --inputbox | ||
+ | --inputmenu | ||
+ | --menu | ||
+ | --mixedform | ||
+ | --mixedgauge | ||
+ | --msgbox | ||
+ | --passwordbox | ||
+ | --passwordform < | ||
+ | --pause | ||
+ | --progressbox | ||
+ | --radiolist | ||
+ | --tailbox | ||
+ | --tailboxbg | ||
+ | --textbox | ||
+ | --timebox | ||
+ | --yesno | ||
+ | </ | ||
+ | |||
+ | ====Customizing Dialogs==== | ||
+ | You can further change the look of dialogs through the options supplied as arguments. | ||
+ | < | ||
+ | [--ascii-lines] [--aspect < | ||
+ | [--begin <y> <x>] [--cancel-label < | ||
+ | [--column-separator < | ||
+ | [--default-item < | ||
+ | [--extra-button] [--extra-label < | ||
+ | [--help-label < | ||
+ | [--insecure] [--item-help] [--keep-tite] [--keep-window] | ||
+ | [--max-input <n>] [--no-cancel] [--no-collapse] [--no-kill] | ||
+ | [--no-label < | ||
+ | [--ok-label < | ||
+ | [--print-maxsize] [--print-size] [--print-version] [--quoted] | ||
+ | [--scrollbar] [--separate-output] [--separate-widget < | ||
+ | [--single-quoted] [--size-err] [--sleep < | ||
+ | [--tab-correct] [--tab-len <n>] [--time-format < | ||
+ | [--title < | ||
+ | [--yes-label < | ||
+ | </ | ||
+ | |||
+ | ====Progress Bars (aka gauge boxes)==== | ||
+ | Dialog/ | ||
+ | |||
+ | According to the **whiptail(1)** manual page: | ||
+ | |||
+ | < | ||
+ | | ||
+ | A gauge box displays a meter along the bottom | ||
+ | indicates | ||
+ | one integer per line. The meter is updated to reflect each new percent- | ||
+ | age. | ||
+ | for a new prompt. | ||
+ | </ | ||
+ | |||
+ | The trick here is to notice that gauge boxes work in conjunction with STDIN. That is, they work because you are feeding them information. No information, | ||
+ | |||
+ | It also literally means having " | ||
+ | |||
+ | This can lead to very disappointing early results, and it can take some searching to find some actual examples demonstrating how to use this functionality. | ||
+ | |||
+ | Here are some working examples to try: | ||
+ | |||
+ | ===Example 1: Simple 1 to 100 counter in Whiptail=== | ||
+ | This can be classified as a "nifty but does nothing" | ||
+ | |||
+ | We'll simply count to 100 in a loop, have a small delay within, and have whiptail work its magic: | ||
+ | |||
+ | <code bash> | ||
+ | for((i=1; i<=100; i++)); do | ||
+ | echo " | ||
+ | sleep 0.24 | ||
+ | done | whiptail --backtitle " | ||
+ | </ | ||
+ | |||
+ | ===Example 2: Processing a group of data=== | ||
+ | Where the gauge can really be useful is in processing files or lines within files. We can make use of the prompt update capabilities and get a really impressive output. | ||
+ | |||
+ | This example will " | ||
+ | |||
+ | <code bash> | ||
+ | cd /dev | ||
+ | cnt=" | ||
+ | pct=" | ||
+ | total=0 | ||
+ | |||
+ | for file in `/bin/ls -1Ad *`; do | ||
+ | total=`echo $total+$pct | bc -l` | ||
+ | echo $total|cut -d' | ||
+ | sleep 0.08 | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | done | dialog --backtitle " | ||
+ | </ | ||
+ | |||
+ | This is an area where the behavior of dialog and whiptail diverge. While you can substitute ' | ||
+ | |||
+ | Ideally you'd only want to put a gauge on a time-consuming process, negating any need for an artificially delaying sleep, as has been used in these examples. | ||
+ | |||
+ | =====Resources===== | ||
+ | |||
+ | * Gauge Boxes | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * https:// | ||
+ | * http:// | ||
+ | |||
+ | <WRAP round note box> | ||
+ | Of particular note to whiptail and gauge boxes, the following statement proved insightful: | ||
+ | |||
+ | It appears that whiptail(1) writes its control output to the termininal based on the setting of the TERM environment variable. Conseqently, | ||
+ | </ |