Informations, tips and technics.

bash and time calculation

I could be fun to have a little time calculation when executing a script within a crontab job, so here is a small one:

#!/bin/ksh# At script beginning, get day of month.
BDAY=$(date +%e)
# Get time in seconds.
((BTIME=($(date +%H)*3600)+($(date +%M)*60)+$(date +%S)))

# script

# At script end, get the end day of month.
EDAY=$(date +%e)
# Add 24hrs in seconds, if days don't match.
# It assumes run time is not over 48 hours.
[[ $BDAY == $EDAY ]] && DAY=0 || DAY=86400
# Get the end time in seconds.
((ETIME=$DAY+($(date +%H)*3600)+($(date +%M)*60)+$(date +%S)))

# Calculate and print the time elapsed.
print "Elasped Time: \c"
((HOURS=TOTAL / 3600))
((TOTAL=TOTAL - (HOURS*3600)))
print "$HOURS hours, $((TOTAL / 60)) minutes, $((TOTAL % 60)) seconds."

Ref : link


Filed under: Bash, Linux, , , ,

Kill me this Zombie Process !!!

Sooner or later, you will have to kill some zombie process. Check Wikipedia (http://en.wikipedia.org/wiki/Zombie_process) to know that are they and why they are your enemies…

 Anyway, some of you may have installed Nagios to monitor those zombie process and a small utility to kill them could be :

 for each in `ps -ef | grep ” | grep -v PID | awk ‘{ print $3 }’`; do for every in `ps -ef | grep $each | grep -v cron | awk ‘{ print $2 }’`; do kill -9 $every; done; done

 Note that you can also create a cron job to do that automatically.

Filed under: Bash, Linux,

Overwrite a console line in Bash

Sometimes in Bash, you want to display some text in the console and sometimes you may also want to have the ability to overwrite the line that you have just displayed with a new string.

For example, a bash script could display a text “Working..” on the console and then, instead of display “Finished!” to the next line, you want the line that first display “Working…” be cleared and replace with the “Finished!” string.

Here is a little piece of code that could do this:

tput sc
echo -n “Working…”
for i in `seq 1 5000`;
echo $i > /dev/null
tput el1
tput rc
echo “Finished!”

As you can see, I use the tput command. Bascally, I use the echo -n command to display the first line of text, the -n parameter to prevent a carriage return (new line), then I save the current line/column position using tput sc. After that, I do some stuff, and then I clear the line on the left using tput el1 and finally restore the position of the line/column using tput rc. I can then write the line that I want, the previous one has been deleted.


Progress bar example.

I have found this example on the net and here is the code. I could help you to acheive the same goal.

ref: http://tldp.org/LDP/abs/html/assortedtips.html#PROGRESSBAR

ref: http://forum.soft32.com/linux/echo-line-overwrite-ftopict470692.html

# progress-bar.sh

# Author: Dotan Barak (very minor revisions by ABS Guide author).
# Used in ABS Guide with permission (thanks!).


# Calculate how many characters will be full.
let “full_limit = ((($1 – $BRACKET_CHARS) * $2) / $LIMIT)”

# Calculate how many characters will be empty.
let “empty_limit = ($1 – $BRACKET_CHARS) – ${full_limit}”

# Prepare the bar.
for ((j=0; j<full_limit; j++)); do

for ((j=0; j<empty_limit; j++)); do


printf “%3d%% %s” $2 ${bar_line}

# Here is a sample of code that uses it.
for ((i=0; i<=MAX_PERCENT; i++)); do
sleep 1
# … Or run some other commands …
print_progress_bar ${BAR_WIDTH} ${i}
echo -en “\r”

echo “”


Filed under: Bash, Linux

Checking OS version on Linux or Unix

I work right now on a bash script utility and I have tried to find a way to check what is my system. The goal of the script is to runs some commands and can be run on RedHat, Suse Linux and Solaris.

I have use this code :

for base in `ls /etc/*release*` ; do
    if [ -f “$base” ]; then
z=$(cat $s | tr [a-z] [A-Z])
id=${z// /}
for critere in $cri; do
    rd=`expr match “$id” “$critere”`
  if [ ! “$rd” -eq 0 ]; then

Assuming that you have a release file in /etc then it should works.

If you are going to ask me why I do not use a findfunction to retrieve all the files in /etc it is because the version of findin Linux and Solaris does not have the same parameters and I can’t use the-maxdeph 1 method to search only in /etc.

So I ended up using an ls function and then test is the result is really a file or not (I do no manage folders and symbolic links). If it is then I can proceed some checks.

Maybe not the best code to know what can of the system the script in running onto but well, it works. But feel free to comment!

Filed under: Bash, Linux

Checking memory on linux with simple scripts

I have found those little scripts very usefull to check memory on a linux system.

Check those scripts here : http://www.oreilly.fr/contenu/2007/04/26/surveiller-la-consommation-m%C3%A9moire-sous-linux

Filed under: Bash, Linux, , , ,

Give your bash scripts some colors

I am pretty sure that every guy that everyone that manage Linux or Unix systems have to deal with bash to create some scripts.

I am currently create one that retrieve some informations on different NIX platforms, like Suns Solaris and Unix.

The goal of this post is not to explain you how to deal with bash but to add some fun to your bash scripts. Adding colors to a scripts could be cool when you have to display important messages, errors or information. Adding colors to those messages make the user more aware of what’s happening and add some eye candy to you script.

We use the echo command with the -e parameter. Here is a small example :


echo -e $COL_BLUEINFO: $COL_RESETThis is an info message
echo -e $COL_REDAn error has occurred$COL_RESET

ref : http://www.bioinspired.com/users/ajg112/software/bashTips.shtml

Filed under: Bash, , ,