Help with Arithmetic calculations in Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Arithmetic calculations in Shell script
# 1  
Old 09-13-2012
Question 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:
Code:
 res=$((100*a/b))
res=`echo "scale=2; $a / $b" | bc`

But I am not getting the decimal values.

My expected result for above is 0.99

And while I testing , I encountered with divide by Zero and the script was hangup. Any idea how to avoid this?

Thanks!
# 2  
Old 09-13-2012
Shell does not give you decimal points. You can multiply ahead to get what you want, though, if you know the result will be 0-99 and not above.
Code:
$ printf "0.%02d\n" $(( (a*10000) / b ))

0.99

$

You forgot the multiply by 100 in your bc statement, so your answer is 0.0099, not 0.99. Do that first in your bc statement (i.e. multiply a by 100, not the result of a/b) and it works fine.

Lastly, the way to avoid divide-by-zero errors is to not divide by zero Smilie Check your values before you calculate.

Last edited by Corona688; 09-13-2012 at 02:04 PM..
# 3  
Old 09-13-2012
Quote:
Originally Posted by Corona688
Shell does not give you decimal points. You can multiply ahead to get what you want, though, if you know the result will be 0-99 and not above.
Code:
$ printf "0.%02d\n" $(( (a*10000) / b ))
 
0.99
 
$

You forgot the multiply by 100 in your bc statement, so your answer is 0.0099, not 0.99. Do that first in your bc statement (i.e. multiply a by 100, not the result of a/b) and it works fine.

Lastly, the way to avoid divide-by-zero errors is to not divide by zero Smilie Check your values before you calculate.
Actually, I thought scale & bc are for division and thinking to multiply the result with 100. But I am wrong. Following worked for me:

Code:
res=`echo "scale=2; 100 * $a / $b" | bc`
echo $res gave me .99
 
res=`echo "scale=3; 100 * $a / $b" | bc`
Now echo $res gave me 0.992

How can I avoid script hangup when divide by Zero happens. Validating the variable is the only way? or any otherway in Unix. Actually I need to display 0 when it happens.

---------- Post updated at 10:12 PM ---------- Previous update was at 10:11 PM ----------

Seems you Edited, while I am replying.... Smilie
# 4  
Old 09-13-2012
Quote:
Originally Posted by karumudi7
How can I avoid script hangup when divide by Zero happens. Validating the variable is the only way? or any otherway in Unix. Actually I need to display 0 when it happens.
Just check. This will turn it into 0/1, which always becomes zero.

Code:
 [ "$b" -eq 0 ] && a=0; [ "$a" -eq 0 ] && b=1

Quote:
Seems you Edited, while I am replying.... Smilie
I'm often sneaky like that. Smilie Sorry.
Quote:
Actually, I thought scale & bc are for division and thinking to multiply the result with 100. But I am wrong.
I think 'scale' in bc does pretty much what I did in shell -- multiplies an extra bunch, then cheats the decimal point. bc is smart enough to know when it'd end up 1, though, when my shell statement would do silly things like 0.100

Last edited by Corona688; 09-13-2012 at 02:32 PM..
# 5  
Old 09-13-2012
Here's a more robust shell version:

Code:
printf "%d.%02d\n" $(( (a*100)/b )) $(( ((a*10000) / b) % 100 ))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mathematical calculations using shell

Dear All, I read some variables in a file and assigned as name for each of them. If I do echo I am able to see the values as 1.0E-05,3.4,5.0E-03 etc, Now I want to do some mathematical operations with them. Lets say 1 1.0E-05*5.0E-03 expected ans is 5.0E-08 2 1.0E-05/5.0E-03 expected... (9 Replies)
Discussion started by: linuxUser_
9 Replies

2. Shell Programming and Scripting

Arithmetic calculations in bash file

I have 2 numbers xmin = 0.369000018 xmax = 0.569000006 and want to calculate (xmax- xmin) / 5.0 I have tried using $(( )) but is always giving an error (8 Replies)
Discussion started by: kristinu
8 Replies

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

4. Shell Programming and Scripting

Date Calculations using script!!

Hi all, Thanks in Advance , i am very new to programming part in script i think using some caluations+ sed command only we can do this process in script. for exampl: i have file in that one line is like this using sed i can replace the date and all but my requirement is The... (3 Replies)
Discussion started by: anishkumarv
3 Replies

5. Homework & Coursework Questions

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (8 Replies)
Discussion started by: Florinel76
8 Replies

6. Shell Programming and Scripting

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (1 Reply)
Discussion started by: Florinel76
1 Replies

7. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: Tirmazi
5 Replies

8. Fedora

Simple arithmetic in shell

Hey, I just wanted to know how one can write simple arithmetic like addition, subtraction, multiplication and division in shell-script. (14 Replies)
Discussion started by: #moveon
14 Replies

9. Shell Programming and Scripting

shell execution time calculations

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script. Does anyone have any examples they have used. I have been setting up two date variables (one at... (1 Reply)
Discussion started by: Omar Bacha
1 Replies

10. Shell Programming and Scripting

problem doing the time calculations in shell.

I am trying to do a shell script to monitor if any files went through in the last hour. There is a script in cron that runs every sec checking to see if a file is there and ftp the file out of this folder. Now I just want to add a block of code that will check to see no files went in the last... (3 Replies)
Discussion started by: jonathan184
3 Replies
Login or Register to Ask a Question