Tuesday, November 4, 2008

Counts from min to max

In order to get counts from start number to end number,
I have written a shell script.


#!/bin/sh

if [ $# -ne 2 ]
then
echo "Usage: $0 min max"
exit
fi


min=$1
max=$2

while [ $min -le $max ]
do
echo -n $min " "
min=`expr $min + 1`
done
#end script



Sample run:
$ ~/bin/count.sh 2 5
2 3 4 5

Tip: This is very handy script, that is very helpful
in getting counts in the loop. For example:
$ for i in `~/bin/count.sh 2 8`
> do
> echo "Hello $i"
> done

Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8

No comments: