1

While I understand how to create menus, no guide I've found teaches how to actually link them to commands

I was hoping to have something like:

dialog--clear --title "n00b's spellbook" \
        --menu "Choose thine spell:" 20 51 4 \
        1 "Name of program"  "blah" \
        2 "Name of other program" "blah" \

then somehow 1 runs ./program1 and 2 runs ./program2

2 Answers 2

1

Sorry. Missed the point. Did not catch it was the dialog package – @jasonwryan fix of the markup made it more clear. – So this is a complete re-write.

As noted by the man pages; I recommend you download the source and look in the examples folder.

If you're in apt-get world simply say:

apt-get source dialog
cd dialog*/samples

A basic example from the yes/no sample (with some modifications):

: ${DIALOG=dialog}

: ${DIALOG_YES=0}
: ${DIALOG_NO=1}
: ${DIALOG_ESC=255}


DIALOG_ERROR=254
export DIALOG_ERROR

$DIALOG --title "YES/NO BOX" --clear "$@" \
        --yesno "A simple Yes/No dialog." 15 61

retval=$?

case $retval in
  $DIALOG_YES)
    echo "YES";;
  $DIALOG_NO)
    echo "NO";;
  $DIALOG_ERROR)
    echo "ERROR!";;
  $DIALOG_ESC)
    echo "ESC pressed.";;
esac
0

The manual page for dialog tells where to start:

--menu text height width menu-height [ tag item ] ...

On exit the tag of the chosen menu entry will be printed on dialog's output.

The given example

dialog--clear --title "n00b's spellbook" \
    --menu "Choose thine spell:" 20 51 4 \
        1 "Name of program"  "blah" \
        2 "Name of other program" "blah" \

appears to be incorrectly formatted since there are three values per line. Reducing that to something more plausible:

dialog --clear --title "n00b's spellbook" \
    --menu "Choose thine spell:" 20 51 4 \
        1 "Name of program" \
        2 "Name of other program"

then the tag values are 1 and 2. By default, dialog's output is the standard error, which can be captured by redirecting that, e.g.,

tempfile=$(mktemp)
dialog --clear --title "n00b's spellbook" \
    --menu "Choose thine spell:" 20 51 4 \
        1 "Name of program" \
        2 "Name of other program" 2> $tempfile

and used in a case-statement:

case `cat $tempfile` in
(1) ./program1;;
(2) ./program2;;
esac

There are several sample scripts in the sources for dialog. The sample scripts execute utility scripts which create and remove the temporary file, as well as allowing testing of environment variables which can change the exit-codes of dialog.

By studying the appropriate scripts—for the menu widget in this case—you can get a better familiarity with dialog.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .