Build Linux Bash Graphical UI Scripts

Tamir Suliman
6 min readSep 23, 2018

--

Photo by Arnold Francisca on Unsplash

If you found this article interesting, your support by following steps will help me spread the knowledge to others:

👏 Give the article 50 claps

💻 Follow me

Bash is a “Unix shell”: a command line interface for interacting with the operating system. It is widely available, being the default shell on many GNU/Linux distributions and on Mac OSX, with ports existing for many other systems. A bash shell script is a computer program written in the Bash programming language.[source wikipedia]

Please review this article about Shell Scripting to get some basic understanding of what shell scripting is all about.

Many programming languages have a way of a coding graphical interface. Luckily, there is a way to code a GUI for shell scripts on bash in the script itself.

In this article, I will introduce whiptail and some examples of how to incorporate it into a shell script.

Whiptail

Whiptail is a program that will let you present a variety of GUI displays or display messages using dialog boxes from a shell script.

How to install?

On Linux ( Redhat , Fedora , CentOS) variants

$ yum install newt -y

On Ubuntu

sudo apt-get install whiptail

On MAC OS

$ brew install newt

So, with whiptail, you could create infobox, message box, yes/ no, and progress gauge in addition to other options.

We are not interested in showing, the options more than showing practical examples and put it to use with some of the shell scripts available there.

So lets’ get started.

Whiptail Examples

ACME company tasked you as their Linux system administrators to create whiptail scripts for ACME dev-ops test environment with the following requirements:

  1. The app will use Progress gauge to evaluate a process or a Linux operation such as find.

The Instructions: ( locating a file in linux root system using bash gui whiptail)

# Create the file 
> touch /etc/fileapp
# Create the script
> touch filelocatorgui.bash
> chmod 0700 filelocatorgui.bash
#!/bin/bashfind /root -name fileapp -type f  &
{
i="0"
while (true)
do
proc=$(ps aux | grep -v grep | grep -e "find")
if [[ "$proc" == "" ]]; then break; fi
sleep 1
echo $i
i=$(expr $i + 1)
done
echo 100
sleep 2
exit 1
} | whiptail --title "FileFind" --gauge "Finding Files.." 8 78 0
# Run the file 

> ./filelocatorgui.bash

The Output:

whiptail — gauge

2. The app will use the message box

This can be used as a message to alert the user to what is coming next. It waits for the user to hit the OK button.

The Script:

#!/bin/bashwhiptail --title "FileFind" --msgbox "This is our ACME-FINDAPP You must hit OK to continue." 8 78
.....

The output :

3. The app will use Input Box and Textbox

The user will enter his/her name then a text box will display the result.

The Script:

#!/bin/bashNAME=$(whiptail --inputbox "What is your Name?" 8 78 Name --title "Name-Display APP" 3>&1 1>&2 2>&3)exitstatus=$?if [ $exitstatus = 0 ]; then
echo "User selected Ok and entered " $NAME
echo "Welcome $NAME" > name_test whiptail --textbox name_test 12 80else
echo "User selected Cancel."
fi
echo "(Exit status was $exitstatus)"

The Output:

Input Box
Message Box

Dialog

Dialog is a similar tool to whiptail and it also used to display dialog boxes from shell scripts

How to install

On Red Hat

sudo dnf install dialog -y

On Ubuntu

sudo apt-get install dialog

Dialog Examples

Adapting the same script from whiptail with dialog can be done as follows:

#!/bin/bash
find /root -name fileapp -type f &

{
i="0"
while (true)
do
proc=$(ps aux | grep -v grep | grep -e "find")
if [[ "$proc" == "" ]]; then break; fi
sleep 1
echo "XXX"
echo $i
echo "Finding Files.. (This may take a while)"
echo "XXX"
# Increment the progress
i=$(expr $i + 10) # Adjust the increment rate as needed
done
echo "XXX"
echo "100"
echo "Find operation completed."
echo "XXX"
sleep 2
} | dialog --title "FileFind" --gauge "Please wait" 8 70 0

The output of the script:

To Display a message with dialog

#!/bin/bash
dialog --title "FileFind" --msgbox "This is our ACME-FINDAPP. You must hit OK to continue." 8 78

The result can be listed as follows:

Now to adapt a text box and a message using dialog where he user will enter his/her name then a text box will display the result

#!/bin/bash
dialog --title "Name-Display APP" --inputbox "What is your Name?" 8 78 2>name_input.txt
exitstatus=$?

if [ $exitstatus = 0 ]; then
NAME=$(<name_input.txt)
echo "User selected Ok and entered $NAME"
echo "Welcome $NAME" > name_test
dialog --textbox name_test 12 80
else
echo "User selected Cancel."
fi

echo "(Exit status was $exitstatus)"
rm -f name_input.txt name_test

Use Case : Files and Directory Backups

Goal: Create a script utilizing the GUI to backup files and directories.

Its important to make sure that the user is running the script has access to the source and destination directory.

Scenario: Upon executing the script the GUI would prompt the user to select a source directory then confirms if this is the right directory if not then it will ask again to select the source directory or quit the program. Once the source directory is selected the user should select the destination directory. Following that you will hit Next to chose the type of compression tar , or zip. The script would then execute the backup process and display a progress bar . After completion a message box could inform the use that the backup is complete.

Results :

The script prompts you to enter the path of the source directory

The script will then ask you to enter the path of the destination directory

Clicking on will generate a successful message

Its a simple script utilizing all the dialog techniques learnt

#!/bin/bash

prompt_directory() {
local title="$1"
local dir=$(dialog --stdout --title "$title Directory Selection" --inputbox "Enter the path of the $title directory:" 10 60)
if [ $? -ne 0 ]; then
dialog --stdout --title "Operation Cancelled" --msgbox "Operation cancelled by the user." 5 50
exit 1
fi
echo "$dir"
}

perform_backup() {
local sourceDir=$(prompt_directory "Source")
local destDir=$(prompt_directory "Destination")
local backupFile="$destDir/backup_$(date +%Y%m%d_%H%M%S).tar.gz"

if tar -czf "$backupFile" -C "$sourceDir" . 2>backup_error.log; then
dialog --stdout --title "Backup Successful" --msgbox "Backup was successfully created at:\n$backupFile" 10 60
else
dialog --stdout --title "Backup Failed" --msgbox "Backup process failed. Check backup_error.log for details." 10 60
fi
}

perform_backup

Conclusion

Graphical user interface GUI becomes a very integral part in designing Linux computer applications. It can provide a platform that users can interact with, for the tasks of gathering and producing information.

GUI makes interacting with programs and applications if designed well more intuitive and easier. It can provide feedback more effectively as the program runs, and can respond to interaction on the fly.

Bash comes with many other GUI tools, in addition to “whiptail” such as “dialog” which can be used to make programming and executing tasks within Linux much easier and fun to work with.

Sources:

  1. https://en.wikibooks.org/wiki/Bash_Shell_Scripting/Whiptail
  2. https://linux.die.net/man/1/whiptail
  3. https://linux.die.net/man/1/dialog

--

--

Tamir Suliman
Tamir Suliman

Written by Tamir Suliman

Writer, Engineer, Cyber security enthusiast ,PhD. Candidate & 4 Open Source write about my day to day experience in Software Data, and Engineering.