Bash Script and Math

I want this to come out to .01 but it keeps coming out to 0
What am I doing wrong?


whatislefta is 0 when I run this it should be .01

Thanks for any help!!
LVL 14
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bash only does integer arithmetic, for floating point use bc -

whatislefta=`echo "200/20000"|bc -l`

Open in new window

You can only do integer maths in bash.

whatislefta=$(echo 'scale=2;200/2000'|bc)
To control floating points, you can use the printf, that is derived from C -

printf "%.3f\n" $whatislefta

Open in new window

The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Rajesh NandanwarCommented:
Float value in a variablewhatislefta=$(echo '200/20000' | bc -l)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
What would you wish to do with a .01 result?
Gerwin Jansen, EE MVETopic Advisor Commented:
You could strip the trailing zeros like this:
a=$(echo '200/20000' | bc -l | sed "s/[0]*$//") ; echo $a

Open in new window

RickEpnetAuthor Commented:
OK this seems to work but down the script I have some if statements and it throws an error it says  " line 95: [: .01: integer expression expected"

Here is the if statement.  $warn is an integer.

   if [ $whatislefta -gt $warn ]
instead of
if [ $((200/20000)) -gt $warn ]
you can do
if [ 200 -gt $(($warn*20000)) ]
RickEpnetAuthor Commented:
I have work out over the next 2 days so I may not be able to look at this until Friday.

Thanks I will let you know as soon as I have time to look at this again.
if you have a decent expr version around, it will handle floating point arithmetic using the '-e' switch

you'd probably work it out much easier by multiplying your initial number and the treshold by 1000 or more and work with integers
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.