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

Actually I decided on this as a final version unless someone can see any problems with it...

#! /bin/sh

slash="/"
filename1="tr"
filename2="bc"

value=$1
inbase=$2
outbase=$3

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

IFS=':'
set $PATH


searchit()#see if required programs - tr and bc can be found
{
	for mypath in $PATH
	do
	
		if [ -f $mypath${slash}${1} ]
		then
			#echo "found it - $mypath${1}"
			return 0
		fi
	done
	echo "could not find $1"
	echo "please check to see that $1 is installed"
	return 1
}

if  searchit "$filename1" && searchit "$filename2"
then
	:
else
	exit 1
fi 

unset IFS

#parameter 1 needs to be uppercase for hex..

val="echo ${value} | tr '[:lower:]' '[:upper:]'"

y=$(eval $val)

bcstr="echo 'obase=${outbase};ibase=${inbase};${y}' | bc"

eval $bcstr

exit 0

Well I think this is the last version...I think or hope. I starting to get a love/hate relationship with shell scripting...its really something but I'm not really sure what...;)

script cnvt

#! /bin/sh

if [ $# -ne 3 ]
then
	echo "usage error - cnvt value inbase outbase"
	exit 1
fi

if which tr > /dev/null 2>&1 && which bc > /dev/null 2>&1
then	
	myupper=$(eval "echo $1 | tr '[:lower:]' '[:upper:]'")
	getconvert="echo 'obase=$3;ibase=$2;$myupper' | bc"
	eval $getconvert
else
	echo "could not find the application(s) 'tr' or 'bc'"
	echo "please check to make sure they are installed correctly"
	exit 1
fi

exit 0

Well I think this is the last version...I think or hope. I starting to get a love/hate relationship with shell scripting...its really something but I'm not really sure what...;)

Are you giving up before achieving an one-liner? ;)

A few points if I may.

When making use of test, get used to of adding always quotes to the variables like you do otherwise.
e.i.

if [ -f "$file" ] # Correct
if [ -f $file ] # Incorrect

In the second case, it could happen to be empty return and test will not get the proper argument and in the first one it would be null string.

Instead of

if [ -f "$file" ] # $file is a regular file

this could be a better check

if [ -x "$file" ] # $file is executable

or both

if [ -f "$file" -a -x "$file" ]

Sometimes you can use bash operators instead of if/elses:

cp oldfile newfile && rm oldfile || echo "Failed to achieve Nirvana"

Displaying the alternative echo only if either cp or rm did not complete successfully
You get the idea.

Thanks for the reply and advice Aia...

Are you giving up before achieving an one-liner?

No...

Isn't funny how languages strive for coherency and function while 'some' programmers strive for one line esoteric marvels...That said, one line esoteric marvels are a wonder to create and show off...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.