Arithmetic calculation on real numbers in Bourne Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic calculation on real numbers in Bourne Shell Script
# 1  
Old 07-07-2010
Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings.

I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers.

Could anyone look at the script and recommend where is the flaw.

Thanks in advance
Code:
#!/bin/sh
echo "Enter the Price of the item \$ \c"
read price
echo "What Percentage of discount is available \c"
read per
echo "Your Total Savings are \c"
sav=`expr \( $per / 100 | bc -l \) \* $price`
echo $sav


Last edited by Franklin52; 07-07-2010 at 02:17 PM.. Reason: Replaced HTML tags with code tags
# 2  
Old 07-07-2010
Either use "expr" or "bc" but not both.
Always do multiply before divide.


Code:
sav=`echo "(($price * $per) / 100)" | bc -l`

# 3  
Old 07-07-2010
Sorry, but that didn't work
# 4  
Old 07-07-2010
What does your current script look like?
What happened when you ran it?
What answers did you give to the questions?


Complete modified script. Only calculation line has changed. I have just tried it in Bourne Shell, Korn Shell and Posix Shell.

Code:
#!/bin/sh
echo "Enter the Price of the item \$ \c"
read price
echo "What Percentage of discount is available \c"
read per
echo "Your Total Savings are \c"
sav=`echo "(($price * $per) / 100)" | bc -l`
echo $sav

This User Gave Thanks to methyl For This Post:
# 5  
Old 07-07-2010
It works when I copied the entire script, may be I had left someting.
Great Thanks

And the following method works too

Quote:
sav=`expr \( $per \* $price \) / 100 `
# 6  
Old 07-07-2010
Tools Another approach

Code:
echo "scale=4; $per * $price / 100" | bc -l

or
Code:
sav=`echo "scale=4; $per * $price / 100" | bc -l`

Sometimes this format is easier to read.
The scale sets for # of decimal places,
then the math equation,
piped to the math calculator.
This User Gave Thanks to joeyg For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BC calculation for floating (invalid arithmetic operator )

Hi, I wish to compare the CPU LOAD 1 min with 5mins and 15mins. If 1 min's CPU LOAd spike 3% compare to 5 mins or 15 mins CPU Load, it is warning. If 1 min's CPU LOAd spike 5% compare to 5 mins or 15 mins CPU Load, it is critical. However I received following code error, I google it and... (10 Replies)
Discussion started by: alvintiow
10 Replies

2. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

3. Shell Programming and Scripting

Arithmetic calculation in variable

Hi, I am trying to do an addition to a value stored in a variable... var1=`grep -n "match" sample.txt|cut -d : -f1` var2=`echo $var1|cut -d " " -f1` (Here i want add +1 to the output value) (4 Replies)
Discussion started by: Kevin Tivoli
4 Replies

4. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

5. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

6. Programming

arithmetic calculation using awk

hi there again, i need to do a simple division with my data with a number of rows. i think i wanted to have a simple output like this one: col1 col2 col3 val1 val2 val1/val2 valn valm valn/valm any suggestion is very much appreciated. thanks much. (2 Replies)
Discussion started by: ida1215
2 Replies

7. Shell Programming and Scripting

calculation using awk or shell script in between the numbers

file A E969K D223L E400L E34L file B predicted 3 1 250 251 500 501 1000 The output should be E969K 501 1000 D223L 1 250 E400L 251 500 E34L 1 250 I tried in this way (1 Reply)
Discussion started by: cdfd123
1 Replies

8. Shell Programming and Scripting

cd from a Bourne Shell Script - Please Help

Dear Bourne Shell Expert, I am trying to change the current working directory from within a Bourne Shell script. Simply enough i thought ! As I am sure you are well aware, Inside the script i echo `pwd` and it seems ok, but the shell spawns another shell to execute this and as such, when my... (10 Replies)
Discussion started by: fawqati
10 Replies

9. UNIX for Dummies Questions & Answers

Bourne Shell Script

Hello, I'm throwing this out there as a novice to the Unix world...I've been working on a project that requires me to ouput (using the echo command) a list of names in a single column format, but the problem is the input is in row format followed by a blank space...If anyone could give me a... (2 Replies)
Discussion started by: dmhonor914
2 Replies

10. UNIX for Advanced & Expert Users

Bourne shell script need help please ?

i have this assignment.. and i mad this script but there is something wrong with it.. if anyone can tell me.. watz going on... i would appreciate it.. tHnX in advance.. count=1 val=$2 op=$1 ans=0 if then if then while do ... (7 Replies)
Discussion started by: dezithug
7 Replies
Login or Register to Ask a Question