Differences between revisions 12 and 14 (spanning 2 versions)
Revision 12 as of 2016-01-25 11:34:14
Size: 3113
Editor: geirha
Comment: printf instead of echo, and "simplifying" the last example
Revision 14 as of 2022-04-13 15:05:34
Size: 3163
Editor: emanuele6
Comment: add syntax highlighting. fix indentation. `unset' -> `unset -v'.
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
{{{
  # POSIX
  foo=$(dialog --inputbox "text goes here" 8 40 2>&1 >/dev/tty)
  printf "The user typed '%s'\n" "$foo"
{{{#!highlight bash
# POSIX
foo=$(dialog --inputbox "text goes here" 8 40 2>&1 >/dev/tty)
printf "The user typed '%s'\n" "$foo"
Line 21: Line 21:
{{{ {{{#!highlight bash
Line 23: Line 23:
unset m; i=0 unset -v m; i=0
Line 36: Line 36:
{{{ {{{#!highlight bash
Line 47: Line 47:
{{{ {{{#!highlight bash
Line 62: Line 62:
{{{ {{{#!highlight bash
Line 67: Line 67:
    for i in 1 2 3; do      for i in 1 2 3; do
Line 69: Line 69:
        sleep "$(( RANDOM % 3 + 1 ))"         sleep 1
Line 71: Line 71:
    

How do I use dialog to get input from the user?

Here is an example:

   1 # POSIX
   2 foo=$(dialog --inputbox "text goes here" 8 40 2>&1 >/dev/tty)
   3 printf "The user typed '%s'\n" "$foo"

The redirection here is a bit tricky.

  1. The foo=$(command) is set up first, so the standard output of the command is being captured by bash.

  2. Inside the command, the 2>&1 causes standard error to be sent to where standard out is going -- in other words, stderr will now be captured.

  3. >/dev/tty sends standard output to the terminal, so the dialog box will be seen by the user. Standard error will still be captured, however.

Another common dialog(1)-related question is how to dynamically generate a dialog command that has items which must be quoted (either because they're empty strings, or because they contain internal white space). One can use eval for that purpose, but the cleanest way to achieve this goal is to use an array.

   1 # Bash
   2 unset -v m; i=0
   3 words=(apple banana cherry "dog droppings")
   4 for w in "${words[@]}"; do
   5     m[i++]=$w; m[i++]=""
   6 done
   7 dialog --menu "Which one?" 12 70 9 "${m[@]}"

In this example, the while loop that populates the m array could have been reading from a pipeline, a file, etc.

Recall that the construction "${m[@]}" expands to the entire contents of an array, but with each element implicitly quoted. It's analogous to the "$@" construct for handling positional parameters. For more details, see FAQ #50.

Newer versions of bash have a slightly prettier syntax for appending elements to an array:

   1 # Bash 3.1 and up
   2 ...
   3 for w in "${words[@]}"; do
   4     m+=("$w" "")
   5 done
   6 ...

Here's another example, using filenames:

   1 # Bash
   2 files=(*.mp3)       # These may contain spaces, apostrophes, etc.
   3 cmd=(dialog --menu "Select one:" 22 76 16)
   4 i=0 n=${#cmd[*]}
   5 for i in "${!files[@]}"; do
   6     cmd[n++]=$i; cmd[n++]=${files[i]}
   7 done
   8 choice=$("${cmd[@]}" 2>&1 >/dev/tty)
   9 printf "Here's the file you chose:\n"
  10 ls -ld -- "${files[choice]}"

A separate but useful function of dialog is to track progress of a process that produces output. Below is an example that uses dialog to track processes writing to a log file. In the dialog window, there is a tailbox where output is stored, and a msgbox with a clickable Quit. Clicking quit will cause trap to execute, removing the tempfile, and destroying the tail process.

   1 # POSIX
   2 # you cannot tail a nonexistent file, so always ensure it pre-exists!
   3 > dialog-tail.log
   4 {
   5     for i in 1 2 3; do
   6         printf '%d\n' "$i"
   7         sleep 1
   8     done
   9 
  10     printf 'Done\n'
  11 } > dialog-tail.log &
  12 
  13 dialog --title "TAIL BOXES" \
  14        --begin 10 10 --tailboxbg dialog-tail.log 8 58 \
  15        --and-widget \
  16        --begin 3 10 --msgbox "Press OK " 5 30
  17 
  18 wait

For an example of creating a progress bar using dialog --gauge, see FAQ #44.


CategoryShell

BashFAQ/040 (last edited 2022-04-13 15:05:34 by emanuele6)