LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-19-2004, 07:08 PM   #1
tw001_tw
Member
 
Registered: Mar 2003
Location: St. Louis, MO
Distribution: kubuntu-current
Posts: 551
Blog Entries: 4

Rep: Reputation: 31
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
 
Old 12-19-2004, 08:02 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The shell only calculates in integers... try using bc tool.
 
Old 12-19-2004, 08:19 PM   #3
tw001_tw
Member
 
Registered: Mar 2003
Location: St. Louis, MO
Distribution: kubuntu-current
Posts: 551

Original Poster
Blog Entries: 4

Rep: Reputation: 31
"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.

Last edited by tw001_tw; 12-19-2004 at 08:21 PM.
 
Old 12-19-2004, 08:20 PM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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.
 
Old 12-19-2004, 08:31 PM   #5
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
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
 
Old 12-19-2004, 09:02 PM   #6
tw001_tw
Member
 
Registered: Mar 2003
Location: St. Louis, MO
Distribution: kubuntu-current
Posts: 551

Original Poster
Blog Entries: 4

Rep: Reputation: 31
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple bash script lp449 Linux - Networking 3 08-02-2005 04:25 PM
Simple bash script Soulful93 Programming 1 06-04-2005 07:26 PM
Simple Bash Script Help njdownes Programming 2 03-05-2005 07:35 AM
Simple Bash Script dmedici Programming 9 12-31-2004 03:48 AM
Simple BASH script fio Linux - General 5 01-18-2003 03:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration