bashdialog

Bash dialog: how do I chain widgets, passing data from one to the next?


I'm trying to build a simple dialog where the user is asked to make a choice (with a menu widget) and then confirm it (with a yesno widget). The separate confirmation is required (preferred, anyway) because any following actions format your disk, which could lead to complaints.

Chaining widgets is easy enough, but passing selections from a menu to downstream widgets is non-trivial. Can anyone suggest a way to do this? In this attempt, the menu output goes to fd 3 (--output-fd won't take a pipe name), and I attempt to read fd 3 for the second widget:

#!/bin/bash

exec 3<> /tmp/fubar
confirm=$(cat /dev/fd/3 &)

res=$(dialog --clear \
       --output-fd 3 \
       --backtitle "Backtitle"  \
       --title "Title" \
       --menu "Please select:" 15 30 4 \
       "1" "Foo" \
       "2" "Bar" \
       --clear   \
       --stderr  \
       --yesno "$confirm" 15 30 \
       2>&1 >/dev/tty)

This doesn't work, of course, not least because $confirm is expanded early. I also need to build a confirmation string instead of just displaying an integer in the yesno box. Any ideas?

EDIT

Minor variation of Gilles' answer below, to use a var instead of a file:

#!/bin/bash

choice=$(dialog --clear \
    --backtitle "Backtitle" \
    --title "Title" \
    --menu "Please select:" 15 30 4 \
    "1" "Foo" \
    "2" "Bar" \
    2>&1 >/dev/tty)

case $choice in
    1) label="foo" ;;
    2) label="bar"  ;;
    *) echo >&2 "ERR $choice"; exit 1 ;;
esac

dialog --clear \
    --backtitle "Backtitle" \
    --title "Confirm" \
    --yesno "You selected option $label. Do you want to proceed?" 10 40

Solution

  • Keep it simple stupid!

    #!/bin/bash
    
    trap 'rm -f /tmp/$$_temp' EXIT
    
    dialog --clear \
        --output-fd 1 \
        --backtitle "Backtitle" \
        --title "Title" \
        --menu "Please select:" 15 30 4 \
        "1" "Foo" \
        "2" "Bar" >/tmp/$$_temp
    
    case $(< /tmp/$$_temp) in
        1) label=yes ;;
        2) label=no  ;;
        *) echo >&2 "ERR $choice"; exit 1 ;;
    esac
    
    dialog --clear \
        --backtitle "Backtitle" \
        --title "Confirm" \
        --yesno "You selected option $label. Do you want to proceed?" 10 40
    [...]