Archive

Posts Tagged ‘dialog’

Whiptail – Using a progress gauge

September 26, 2012 5 comments

Problem:
A problem I faced with whiptail was how to use the progress gauge. Most examples I had seen were not very helpful for what I had in mind. My issue was that I needed the progress gauge to show the script user how far a particular activity had gone. In my case the activity was dumping a mysql database.

Solution:
1. Here is simple example of a progress gauge.

#!/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

2. Here is the way I finally managed to get this done.

#!/bin/bash

# Start mysqldump and send it to the background.
mysqldump --user="root" --password="password" database > database.sql & 
# Keep checking if the process is running. And keep a count.
{
        i="0"
        while (true)
        do
            proc=$(ps aux | grep -v grep | grep -e "mysqldump")
            if [[ "$proc" == "" ]]; then break; fi
            # Sleep for a longer period if the database is really big 
            # as dumping will take longer.
            sleep 1
            echo $i
            i=$(expr $i + 1)
        done
        # If it is done then display 100%
        echo 100
        # Give it some time to display the progress to the user.
        sleep 2
} | whiptail --title "Backup" --gauge "Dumping database" 8 78 0

Update:
Improved progress gauge code.

Source:
http://www.dawoodfall.net/files/scripts/bash/wget-dialog-example
http://www.fifi.org/doc/dialog/examples/
https://www.linuxquestions.org/questions/programming-9/dialog-gauge-how-to-display-progress-of-compile-broken-pipe-problem-200614/

Advertisements
Categories: bash Tags: ,

Whiptail

September 7, 2012 Leave a comment

Problem:
I have to automate a backup process, but I need to have a GUI front-end since the users are very intimidated by the bash prompt.

Solution:
Since the script will most likely go on a server without X I decided on Whiptail. Whiptail is a dialog (software) replacement using Newt instead of ncurses.

Tips on whiptail usage:
1. http://stackoverflow.com/questions/1562666/bash-scripts-whiptail-file-select
2. http://en.wikibooks.org/wiki/Bash_Shell_Scripting/Whiptail
3. http://www.techrepublic.com/blog/opensource/how-to-use-whiptail-to-write-interactive-shell-scripts/3188
4. http://www.dawoodfall.net/scripts/bash/wget-dialog-example

Categories: bash Tags: , ,