Terminal – How to Create a Terminal-Based GUI

terminaltext-user-interface

I'm looking to create a terminal-based environment to adapt my Bash script into. I want it to look like this:

Debian install

Best Answer

dialog --backtitle "Package configuration" \
       --title "Configuration sun-java-jre" \
       --yesno "\nBla bla bla...\n\nDo you accept?" 10 30

enter image description here

The user response is stored in the exit code, so can be printed as usual: echo $? (note that 0 means "yes", and 1 is "no" in the shell world).


Concerning other questions from the comment section:

  • to put into the dialog box the output from some command just use command substitution mechanism $(), eg:

     dialog --backtitle "$(echo abc)" --title "$(cat file)" ...
    
  • to give user multiple choices you can use --menu option instead of --yesno

  • to store the output of the user choice into variable one needs to use --stdout option or change output descriptor either via --output-fd or manually, e.g.:

    output=$(dialog --backtitle "Package configuration" \
                    --title "Configuration sun-java-jre" \
                    --menu "$(parted -l)" 15 40 4 1 "sda1" 2 "sda2" 3 "sda3" \
             3>&1 1>&2 2>&3 3>&-)
    echo "$output"
    

    This trick is needed because dialog by default outputs to stderr, not stdout.

And as always, man dialog is your friend.

Related Question