Skip to content

Instantly share code, notes, and snippets.

@alien11689 alien11689/whiptailDemo.sh
Last active Feb 24, 2019

Embed
What would you like to do?
title="Whiptail DEMO"
responseFile=/tmp/demoResponse
whiptail --title "$title" --msgbox "Hi, I'm whiptail" 8 40
function printMenu {
whiptail --title "$title" --menu \
"What do you want to see?" 30 78 10 \
"MESSAGE_BOX" "Maybe message box?" \
"YES_NO_BOX" "Maybe yes/no box?" \
"INPUT_BOX" "Maybe input box?" \
"TEXT_BOX" "Maybe text box?" \
"PASSWORD_BOX" "Maybe password box?" \
"CHECK_LIST" "Maybe checklist?" \
"RADIO_LIST" "Maybe radio list?" \
"PROGRESS_BAR" "Maybe progress bar?" 2> $responseFile
}
function getResponse {
cat $responseFile
}
function info {
message=$1
whiptail --title "$title" --msgbox "$1" 8 40
}
function youSay {
response=`getResponse`
if [ -z "$response" ]; then
whiptail --title "$title" --msgbox "You haven't say anything" 8 40
else
whiptail --title "$title" --msgbox "You have said: $response" 8 40
fi
}
function perform {
menuSelect=$1
case $menuSelect in
MESSAGE_BOX) performMessageBox ;;
YES_NO_BOX) performYesNoBox ;;
INPUT_BOX) performInputBox ;;
TEXT_BOX) performTextBox ;;
PASSWORD_BOX) performPasswordBox ;;
CHECK_LIST) performCheckList ;;
RADIO_LIST) performRadioList ;;
PROGRESS_BAR) performProgressBar ;;
*) performUnknown ;;
esac
}
function performMessageBox {
whiptail --title "$title" --msgbox "You can only say OK" 8 40
}
function performYesNoBox {
whiptail --title "$title" --yesno "You have simple choose..." 8 40 && info "You have approved" || info "You have not approved"
}
function performInputBox {
whiptail --title "$title" --inputbox "What is your name?" 8 40 2> $responseFile
youSay
}
function performPasswordBox {
whiptail --title "$title" --passwordbox "What is your password?" 8 40 2> $responseFile
youSay
}
function performCheckList {
whiptail --title "$title" --checklist "What project do you know?" 20 60 3 \
"PROJECT1" "Do you know project1?" "NO" \
"PROJECT2" "Do you know project1?" "YES" \
"PROJECT3" "Do you know project1?" "NO" 2> $responseFile
youSay
}
function performRadioList {
whiptail --title "$title" --radiolist "What is your favourite project?" 20 60 3 \
"PROJECT1" "Project 1 is the best" "NO" \
"PROJECT2" "Project 2 is the best" "YES" \
"PROJECT3" "Project 3 is the best" "NO" 2> $responseFile
youSay
}
function performTextBox {
f=/tmp/bla
echo -n "" > $f
for x in `seq 1 1000` ; do
echo "Line $x" >> $f
done
whiptail --title "$title" --textbox $f 20 40 --scrolltext
}
function performProgressBar {
for x in `seq 1 100`; do
sleep 0.05
echo $x
done | whiptail --gauge "5 seconds break" 6 50 0
whiptail --title "$title" --msgbox "Thank you!" 8 40
}
function performUnknown {
whiptail --title "$title" --msgbox "What???" 8 40
}
function mainLoop {
printMenu
menuSelect=`getResponse`
if [ "x$menuSelect" == "x" ]; then
whiptail --title "$title" --msgbox "Bye, bye" 8 40
else
perform $menuSelect
mainLoop
fi
}
mainLoop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.