classed to classless netmask conversion

der.hans PLUGd@LuftHans.com
Mon, 17 Apr 2000 04:14:28 -0700 (MST)


moin, moin,

needed to take a normal dotted quad netmask to classless for something and
thought others might like this little script as well.

I use it as a function, but am including it here as a standalong script.

Send bug reports and enhancements to the list or to PLUGd@LuftHans.com :).

#!/bin/bash

classed_2_classless()
{
  decimal='0'
  for i in `echo $1 | tr \. " "`
  do
    if [ $i != '0' ] ; then
      i=`expr $i + 1`
    fi
    decimal=`expr $decimal + $i`
  done
  # classless
  echo `expr $decimal / 32`
}

netmask=$1

classlessmask=`classed_2_classless $netmask`

echo $classlessmask

ciao,

der.hans
-- 
#  der.hans@LuftHans.com   home.pages.de/~lufthans/   www.OpNIX.com
#  I've got a photographic memory,
#  but I'm lousy photographer. - der.hans