On Thu, 27 Jan 2000, Jeff Smelser wrote:

> Just trying to write a bash script and can't seem to figure out how to
> do addition with variables. If someone could give me a quick example, I
> would appreciate it. Thanks

Here is a script with lots of math examples.

------ cut ------

#!/bin/ksh
# Usage: ip2n <IP-Number>

Die() {
        echo Bad IP
        exit 1
}

[ $# = 1 ] || Die

[ ${1%%?*.?*.?*.?*} ] && Die

I=$1

until [ ${#I} = 0 ]
do
        J=${I#[0-9.]}
        [ ${#I} = ${#J} ] && Die
        I=$J
done

I=$1.

N=0

while [ $I ]
do
        J=${I%%.*}
        [ $((J>>8)) = 0 ] || Die
        N=$(( (N<<8) + J ))
        I=${I#$J.}
done

if [ $((N < 0)) = 0 ]
then
        echo $N
else
        d=100000
        m=$(( (1<<31) - 1 ))
        u=$(( m/d ))
        l=$(( m - u*d ))

        v=$(( (1<<31) + N ))
        x=$(( v/d ))
        y=$(( v - x*d ))

        s=$(( l + y + 1 ))
        c=$(( s/d ))

        ux=$(( u + x + c ))
        ly=$(( s - c*d ))

        echo ${ux}${ly}
fi

# End

------ cut ------

Cut the above and put it in a file called "ip2n", then do
the "chmod +x" thing on it then run it with the following:

        ip2n 192.168.1.1

or any other IP of your choice.



-- 
Your object is to save the world, while still leading a pleasant life.

 9:42pm up 11 days, 14:11, 6 users, load average: 0.00, 0.00, 0.00


-- 
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.

Reply via email to