More Whiptail.

Here are some more examples on using whiptail expanding on what we did in the last article.

A message box shows any arbitrary text message with a confirmation button to continue. whiptail --title "<message box title>" --msgbox "<text to show>" <height> <width>


Example: #!/bin/bash whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60

Example:

#!/bin/bash
 whiptail --title "Message Box" --msgbox "Create a message box with whiptail.
Choose Ok to continue." 10 60

Screenshot from 2015-02-07 12:22:20


Create a Yes/No Box

One common user input is Yes or No. This is when a Yes/No dialog box can be used. whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>

#!/bin/bash
if (whiptail --title "Yes/No Box" --yesno "Do you like computers (yes/no)?" 10 60)
then echo "Yes, you like computers. Exit status was $?."
else echo "No, you do not like computers. Exit status was $?."
fi



Optionally, you can customize the text for Yes and No buttons with "--yes-button" and "--no-button" options.

Example:

 #!/bin/bash

if (whiptail --title "Test Yes/No Box" --yes-button "Free software" --no-button "Closed software" --yesno "Which do you like better?" 10 60)

then echo "You chose free software Exit status was $?."

else echo "You chose closed software. Exit status was $?."

fi





You saw the freeform and the password boxes in the previous sections.


Create a Menu Box
When you want to ask a user to choose one among any arbitrary number of choices, you can use a menu box. whiptail –title “<menu title>” –menu “<text to show>” <height> <width> <menu height> [ <tag> <item> ] . . .

Example:

#!/bin/bash
OPTION=$(whiptail –title “Menu Dialog” –menu “Choose your option” 15 60 4 \ “1” “Grilled ham” \ “2” “Swiss Cheese” \ “3” “Charcoal cooked Chicken thighs” \ “4” “Baked potatos” 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ];
then echo “Your chosen option:” $OPTION
else echo “You chose Cancel.”
fi

Screenshot from 2015-02-07 12:13:57






Create a Radiolist Dialog
A radiolist box is similar to a menu box in the sense that you can choose only option among a list of available options. Unlike a menu box, however, you can indicate which option is selected by default by specifying its status. whiptail –title “<radiolist title>” –radiolist “<text to show>” <height> <width> <list height> [ <tag> <item> <status> ] . . .

Example:

#!/bin/bash
DISTROS=$(whiptail –title “Test Checklist Dialog” –radiolist \ “What is the Linux distro of your choice?” 15 60 4 \ “Debian” “Stable Debian” ON \ “Ubuntu” “Copycat Debian” OFF \ “Centos” “Copycate Redhat” OFF \ “Mint” “Copycat Ubuntu/Debian” OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ];
then echo “The chosen distro is:” $DISTROS
else echo “You chose Cancel.”
fi

Screenshot from 2015-02-07 12:04:52

Create a Checklist Dialog

A checklist dialog is useful when you want to ask a user to choose more than one option among a list of options, which is in contrast to a radiolist box which allows only one selection. whiptail --title "<checklist title>" --checklist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .


Example:
#!/bin/bash

DISTROS=$(whiptail --title "Test Checklist Dialog" --checklist \ "Choose preferred Linux distros" 15 60 4 \ "Debian" "Stable Debian" ON \ "Ubuntu" "Debian copycat" OFF \ "Centos" "Redhat copycat" ON \ "Mint" "Copycat Ubuntu/Debian" OFF 3>&1 1>&2 2>&3)

exitstatus=$?

if [ $exitstatus = 0 ];

then echo "Your favorite distros are:" $DISTROS

else echo "You chose Cancel."

fi



Create a Progress Bar
Another user-friendly dialog box is a progress bar. whiptail reads from standard input a percentage number (0 to 100) and displays a meter inside a gauge box accordingly. whiptail --gauge "<test to show>" <height> <width> <inital percent>
#!/bin/bash
PCT=0
(
while test $PCT != 100;
do
PCT=`expr $PCT + 10`;
echo $PCT;
sleep 1;
done; ) | whiptail --title "GAUGE" --gauge "Hi, this is a gauge widget" 20 70 0
Screenshot from 2015-02-07 11:40:30
By now, you must see how easy it is to create useful dialog boxes in an interactive shell script. Next time you need to write a shell script for someone, why don't you try whiptail

Comments

Popular posts from this blog

Guiless?

MSOffice vs Libreoffice

Raspberry pi zero.