Skip to content

Instantly share code, notes, and snippets.

@gpsoft
Created April 19, 2018 14:38
Show Gist options
  • Select an option

  • Save gpsoft/cc000c0a91f9b1bce8c5cd7c13c41322 to your computer and use it in GitHub Desktop.

Select an option

Save gpsoft/cc000c0a91f9b1bce8c5cd7c13c41322 to your computer and use it in GitHub Desktop.
How to use `dialog` command in a shell script.
#!/usr/bin/bash
dialog \
--title "Yeah!" \
--msgbox "Test for dialog.\nPress OK." \
7 60
dialog \
--title "Excuse me?" \
--yesno "Are you still there?" \
0 0
res=$?
if [ $res -ne 0 ]; then
clear
echo "Okay bye."
exit $res
fi
inputBox() {
local prompt=${1-"Enter"}
local box=${2-"inputbox"}
local inp=
local warn=
while [ -z "$inp" ]
do
# Swapping stdout and stderr.
# `dialog` uses stderr for printing the result
# while we want it in stdout.
inp=$(dialog \
--${box} "${prompt}\n${warn}" \
10 60 3>&1 1>&2 2>&3 3>&-)
res=$?
if [ $res -ne 0 ]; then
return $res
fi
if [ -z "$inp" ]; then
warn="Enter something *NOT* empty."
fi
done
echo $inp
}
name=$(inputBox "Enter your name.")
res=$?
if [ $res -ne 0 ]; then
clear
echo "See you later."
exit $res
fi
pw=$(inputBox "Enter your password." passwordbox)
res=$?
if [ $res -ne 0 ]; then
clear
echo "See you later."
exit $res
fi
choice=$(dialog \
--no-cancel \
--menu "What would you like?" \
10 60 3 1 apple 2 orange 3 banana 3>&1 1>&2 2>&3 3>&-)
dialog \
--infobox "Okay, now let me think it over..." 5 60
sleep 3
clear
echo "Hello, $name"
echo "You selected #${choice}, right?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment