This page looks best with JavaScript enabled

Dialog bash script

 ·  🎃 kr0m

When programming scripts, it can be useful to create menus. In this article, we will learn how to use dialog, a very simple tool that generates very attractive menus.

We install the software:

pkg install xdialog
emerge -av dev-util/dialog

Creamos el script de ejemplo:

vi dialog.sh

#!/usr/bin/env bash

HEIGHT=15
WIDTH=40
CHOICE_HEIGHT=4
BACKTITLE="Backtitle here"
TITLE="Title here"
MENU="Choose one of the following options:"

OPTIONS=(1 "Option 1"
         2 "Option 2"
         3 "Option 3")

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear
case $CHOICE in
        1)
            echo "You chose Option 1"
            ;;
        2)
            echo "You chose Option 2"
            ;;
        3)
            echo "You chose Option 3"
            ;;
esac

We assign execution permissions:

chmod 700 dialog.sh

Finally, we run it:

./dialog.sh

The final result is this:

If you liked the article, you can treat me to a RedBull here