Link to home
Start Free TrialLog in
Avatar of RickEpnet
RickEpnetFlag for United States of America

asked on

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=$((200/20000))

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

Thanks for any help!!
Avatar of Mazdajai
Mazdajai
Flag of United States of America image

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

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

Open in new window

Avatar of Tintin
Tintin

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

ASKER CERTIFIED SOLUTION
Avatar of Rajesh Nandanwar
Rajesh Nandanwar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What would you wish to do with a .01 result?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RickEpnet

ASKER

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 ]
   then
      ex=0
      condition="OK"
instead of
if [ $((200/20000)) -gt $warn ]
you can do
if [ 200 -gt $(($warn*20000)) ]
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial