RickEpnet
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!!
What am I doing wrong?
whatislefta=$((200/20000))
whatislefta is 0 when I run this it should be .01
Thanks for any help!!
You can only do integer maths in bash.
whatislefta=$(echo 'scale=2;200/2000'|bc)
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What would you wish to do with a .01 result?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"
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)) ]
if [ $((200/20000)) -gt $warn ]
you can do
if [ 200 -gt $(($warn*20000)) ]
ASKER
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.
Thanks I will let you know as soon as I have time to look at this again.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window