Sunday, June 13, 2010

Simple stopclock using BASH

Today I had to use a stop clock to time something in a program that I was writing. I found a way to use a BASH variable to make my simple watchdog

#!/bin/bash
clear

# While some condition is true (I always tend to use data command)
while date > /dev/null
do
# Seconds is a bash variable which will give the number of seconds since
# the shell invocation. If a value is assigned to SECONDS, the value
# returned upon subsequent references is the number of seconds since
# the assignment plus the value assigned. "man bash" for more details
SECONDS=0

# Something to pause the execution
read pause_var
echo " $SECONDS Secs"
done

I press enter to know the lap time.

BASH has so many such surprises in it, I wonder how much more still needs to be discovered.

No comments: