Friday, April 22, 2011

Script to find sum of a word in numbers

Attitude = 100% , Usually we find this in lot of forums or mailing lists.
To calculate this in Linux the below script can be used.

Pass the word as first argument to calculate its sum in words.

#!/bin/bash

initial=1
for alpha in {a..z}
do
let $alpha=$initial
initial=`expr $initial + 1`
done
word="$1"
length=${#word}
base=0
offset=1
final=0
while [ $base -lt $length ]
do
semi=$(($(echo ${word:base:offset})))
let final=`expr $final + $semi`
let base++
done

echo "Numeric value of $word is $final"