Archive

Archive for September, 2012

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/

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: , ,

bash – redirection

September 4, 2012 1 comment

Problem:
I needed my script to record both stdout and stderr to a log file.

Solution:

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.

grep da * 1>&2

eg:

#!/bin/bash
cd /home/user
python print_to_stdout.py 1>&2>> logfile.log

Update – 2017-02-24
Above method of redirecting does not seem to work so well within scripts. Better to use

cmd >> file.txt 2>&1

Which translates to

Quote – It’s simple redirection, redirection statements are evaluated, as always, from left to right. >>file : Red. STDOUT to file (append mode) (short for 1>>file) 2>&1 : Red. STDERR to “where stdout goes” Note that the interpretion “redirect STDERR to STDOUT” is wrong. – End Quote. By TheBonsai

From -> http://stackoverflow.com/questions/876239/how-can-i-redirect-and-append-both-stdout-and-stderr-to-a-file-with-bash

Source:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

Other Resources:

http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/

Categories: bash Tags: