Interactive Linux Shell Script with whiptail

In this guide, i’ll show you Whiptail,  a tool used to write an interactive shell script.

Check whiptail and it’s option, from command line with: whiptail

 

To use it, make a bash file, named  shellgui1.sh, to create it can use a terminal text editor like vi or nano , personally i prefer nano, to start edit use the  command:

nano shellgui1.sh

then write that code :

#! /bin/bash
whiptail --yesno "Do you know whiptail?" --yes-button "yes, i know" --no-button "No, i don't know" 10 70
CHOICEs=$?

The first row say to Linux witch shell we want to use

The second row implement the whiptail command

The third row , show variable choice that contain our selection (0 if you choose no, 1 if you choose  yes)

Save the file by using the keyboard shortcut:

CTRL + O

then CTRL + X

make the file executable with command :

chmod +x shellgui1.sh

 

execute the script with the command:

./shellgui1.sh 

On terminal can see the yes/no dialog, that can handle with keyboard’s arrows

 

Then if you want show a dialog title on top, can edit the script by adding the option:

#! /bin/bash 
whiptail --title "Make a decision" --yesno "Do you know whiptail?" --yes-button "Yes, i know" --no-button "No, i don't know" 10 70 
CHOICEs=$?

The result:

 

Now, if you want execute an action, can check the choice variable, in this example, we print a string on terminal based on dialog choice, then print:

  • “i know” for yes
  • “i don’t know” for no

Then the updated script:

#! /bin/bash
whiptail --title "MAke a DEcision" --yesno "Do you know whiptail?" --yes-button "Yes, i know" --no-button "No, i don't know" 10 70
CHOICEs=$?
if [ $CHOICEs = 0 ]; then
 echo "i know "
else
 echo "i don't know"
fi

By use the other option listed at the guide begin, we can make a lot of interactive interface for our bash script 🙂

 

Download Tutorial Source From GitHub