#!/bin/bash
help()
{
cat<<HELP
b2d-convert binary to decmial
USEAGE:b2d [-h] binarynum
OPTION:-h text help
EXAMPLE:b2d 111010
will return 58
HELP
exit 0
}
error()
{
echo "$1"
exit 1
}
lastchar()
{
if [ -z "$1" ];then
reval=""
return
fi
numofchar=$(echo -n "$1" | sed 's/ //g'| wc -c)
reval=$(echo -n "$1" | cut -b "$numofchar")
}
chop()
{
if [ -z "$1" ]; then
rval=""
return
fi
numofchar1=$(expr "$numofchar" - 1)
reval=$(echo -n "$1" | cut -b 1-${numofchar1})
}
while [ -n "$1" ];do
case $1 in
-h)help;shift1;;
--)shift;break;;
-*)error "error:no such option $1. -h for help";;
*)break;;
esac
done
sum=0
weight=1
[ -z "$1" ]&&help
binnum="$1"
while [ -n "$binnum" ];do
lastchar "$binnum"
if [ "$reval" = "1" ];then
sum=$(expr "$sum" + "$weight")
echo "sum is ${sum}"
fi
if [ "$binnum" = "0" ];then break; fi
if [ "$binnum" = "1" ];then break; fi
chop $binnum
binnum=$reval
echo "$binnum"
weight=$(expr "$weight" "*" 2)
echo "$weight"
done
echo -e "hello ,$USER.\n\n $1 binry to decmial $sum"
help()
{
cat<<HELP
b2d-convert binary to decmial
USEAGE:b2d [-h] binarynum
OPTION:-h text help
EXAMPLE:b2d 111010
will return 58
HELP
exit 0
}
error()
{
echo "$1"
exit 1
}
lastchar()
{
if [ -z "$1" ];then
reval=""
return
fi
numofchar=$(echo -n "$1" | sed 's/ //g'| wc -c)
reval=$(echo -n "$1" | cut -b "$numofchar")
}
chop()
{
if [ -z "$1" ]; then
rval=""
return
fi
numofchar1=$(expr "$numofchar" - 1)
reval=$(echo -n "$1" | cut -b 1-${numofchar1})
}
while [ -n "$1" ];do
case $1 in
-h)help;shift1;;
--)shift;break;;
-*)error "error:no such option $1. -h for help";;
*)break;;
esac
done
sum=0
weight=1
[ -z "$1" ]&&help
binnum="$1"
while [ -n "$binnum" ];do
lastchar "$binnum"
if [ "$reval" = "1" ];then
sum=$(expr "$sum" + "$weight")
echo "sum is ${sum}"
fi
if [ "$binnum" = "0" ];then break; fi
if [ "$binnum" = "1" ];then break; fi
chop $binnum
binnum=$reval
echo "$binnum"
weight=$(expr "$weight" "*" 2)
echo "$weight"
done
echo -e "hello ,$USER.\n\n $1 binry to decmial $sum"