Help with simple bash script - please
I'm writing a simple bash script. I have just started learning about writing these,
but I have found a need for something, so I set out to do it. The problem is with
Here is my script:
it gets rounded down to zero instead of a decimal. I want the output to be
fractional to 2 decimal places (example - 64/256 = 0.25)
The 2 beginners guides I have do not reference this. I tried searching on line in
advanced guides, but got lost... fast.
I'm sure it has to do with how I am having it caclulate r1, g1, & b1, but I can't find it.
Any script savy people out there willing to help?
Thanks in advance
The shell only calculates in integers... try using bc tool.
"The shell only calculates in integers"
That thought crossed my mind, but I wasn't sure - and I was being hopeful.
I guess that answers my quesiton.
I'm not ready to tackle 'bc' . thanks though.
If you want to store a decimal value instead of an interer value, you need to use modulus instead of integer assignment.
r1 =$ ((r % z)) returns the modulus (remainder) of division, if the result is not an integer. The integer value would be lost, so, if you expect a value which will have both an integer component and a decimal component, you would have to perform two divisions: one to return integer, and one to return remainder.
r1 =$(( r/z)) and r1=$((r % z))
as far as limiting output to two decimal places, I can't help there.
If you want to use bc (as per chrism01's suggestion), you could do something along the lines of
mjrich - 1 word - wow. Thanks.
r1=$(echo "scale=$s; $r/$z" | bc)
g1=$(echo "scale=$s; $g/$z" | bc)
b1=$(echo "scale=$s; $b/$z" | bc)
I don't understand the above lines too well, but since you provided me
with them, I'm gonna figure it out. (watch this thread :) - I might have questions.)
And thanks to all that responded. I tweeked the code a bit and here is the final result.
Once again, LQ came through. Thanks again.
|All times are GMT -5. The time now is 10:32 AM.|