Hi, I wrote this simple bash script(yeah simple now that its done) and it uses bc - 'The arbitrary precision calculator language' and tr - 'translate or delete characters'. Now the script works, it will take input values and convert them to the proper output values. Now my question is..is this the proper way to do it? I'm not very experienced with shell scripting...well anything complicated.
script usage:
cnvt value inbase outbase
example:
cnvt ff 16 2
cnvt takes value ff of base 16 and converts it to base 2.
cnvt
#! /bin/sh
if [ -f /usr/bin/bc ]
then
:
else
echo "Could not find the 'bc' calculator"
echo "please install bc - The arbitrary precision calculator language"
echo "or you may have to create a link - /usr/bin/bc"
exit 1
fi
if [ $# -ne 3 ]
then
echo "usage error - cnvt value inbase outbase"
echo "cnvt - program name"
echo "value - numeric value to convert"
echo "inbase - numeric base of the value to convert"
echo "outbase - numeric base of the converted value"
exit 1
fi
#parameter 1 needs to be uppercase for hex..
val="echo ${1} | tr '[:lower:]' '[:upper:]'"
y=`eval $val`
bcstr="echo 'obase=${3};ibase=${2};${y}' | bc"
eval $bcstr
exit 0