LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help with simple bash script - please (http://www.linuxquestions.org/questions/linux-software-2/help-with-simple-bash-script-please-268198/)

tw001_tw 12-19-2004 07:08 PM

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
division.

Here is my script:
Code:

#!/bin/bash
clear
z=256
echo "What is the Red value?"
read r
echo "What is the Green value?"
read g
echo "What is the Blue value?"
read b
r1=$((r/z))
g1=$((g/z))
b1=$((b/z))
echo "The values are now..."
echo "$r1, $g1, $b1"

The problem is that since most of the time, the ending values will be less then 1,
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
-tw

chrism01 12-19-2004 08:02 PM

The shell only calculates in integers... try using bc tool.

tw001_tw 12-19-2004 08:19 PM

"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.
appreciated.

bigrigdriver 12-19-2004 08:20 PM

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.

mjrich 12-19-2004 08:31 PM

If you want to use bc (as per chrism01's suggestion), you could do something along the lines of
Code:

#!/bin/bash
clear
z=256
s=3    # decimal places required.
echo "What is the Red value?"
read r
echo "What is the Green value?"
read g
echo "What is the Blue value?"
read b
r1=$(echo "scale=$s; $r/$z" | bc)
g1=$(echo "scale=$s; $g/$z" | bc)
b1=$(echo "scale=$s; $b/$z" | bc)
echo "The values are now..."
echo "$r1, $g1, $b1"

Cheers,

mj

tw001_tw 12-19-2004 09:02 PM

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.

Code:

#!/bin/bash
clear
z=256
s=2    # decimal places required.
echo "What is the Red value?"
read r
echo "What is the Green value?"
read g
echo "What is the Blue value?"
read b
r1=$(echo "scale=$s; $r/$z" | bc)
g1=$(echo "scale=$s; $g/$z" | bc)
b1=$(echo "scale=$s; $b/$z" | bc)
echo "The values are now..."
echo "0$r1, 0$g1, 0$b1"

This now provides me with the exact output I need descriped on this page:
http://piglet.uccs.edu/~semwal/viewing.html

Once again, LQ came through. Thanks again.
-tw


All times are GMT -5. The time now is 02:12 AM.