Arithmatic calculation in shell script
I am working with a shell script on a Solaris 10 machine. Shell is /bin/bash.
Script contains some arithmatic calculation, which is actually not working properly. Suppose I have following lines in my script: x=5 y=4 ans=$((x + y)) echo "$ans" Then after invoking script, it gives me following error: <scriptname>: syntax error at line 3: `ans=$' unexpected On the other hand, if I use expr command, then the output is always an interger, not floating point number. For instance, if I use: expr 100 \* 1 / 3 Then it will give, 33, not 33.33.. Moreover, awk is also giving an error, as if I do: awk '{print 100*5/10}' Then it returns either of below errors: awk: syntax error near line 1 awk: illegal statement near line 1 How can I solve this? |
maybe
Code:
let x=5 Edit: I vote "no" on "let..." Sorry about that. |
Code:
#!/usr/bin/env bash Code:
$ bash --version Note: alternatively, you could use Code:
echo '4+5' | bc |
Alright, I have come to a conclusion that it's all about shell that I am using and running my script with. Things are running ok with c shell ie. csh or tcsh, but causing pb if I use #!/bin/sh or #!/bin/bash. So I have made changes into my script according to csh.
|
Well... I still think there's something odd going on, as what you had should have run with bash, but if you consider the problem solved then I'm glad to hear it :)
|
Quote:
Well, now the problem is that the arithmatic calculation isn't showing the floating point number, but it modifies the result of calculation and gives an integer value. So what should one do to get output of some arithmatic calc. to get result in floating point number? |
Arithmatic calculation - Result in floating point numberr
Friends,
What is the best way, I mean, command or expression should we use in a shell script so that any arithmatic calculation result in a floating point number? Let's say, 5*20/3 will give us 33.3333..., but same thing in a shell script results as 33 (integer value). So how to get it as a floating point number? |
hey!
Consider bc, an example: Quote:
Thor |
Check This result
Code:
root@ubuntu-vm:~# echo 5*20/3 | bc -l |
Code:
echo "scale=3; (5*20/3)" | bc -l |
Quote:
For example, if I do: echo 100*5/20 | bc -l It results 25. But if I do: var1=5 var2=20 and echo 100*var1/var2 | bc -l It all the time result "divided by zero" error...error.... and error! I cant figure out that which thing I need to change i.e. shell or command or way of assigning variable.. so I can get proper result. Previous related posts: http://www.linuxquestions.org/questi...pt-4175432684/ http://www.linuxquestions.org/questi...wk-4175432706/ |
It worked with this code. There was tinny mistake with awk code. But now it's working fine in my environments:
#!/bin/bash var1=1111688 var2=374335 echo | awk "BEGIN{print 100 - $var2*100/$var1}" Output: 66.3273 Special thanks to Snark. Thanks a lot everyone!! |
I'm kind of surprised nobody has specifically pointed this out yet: bash arithmetic is currently integer only. Any non-integer values are truncated in output.
arithmetic expressions Pretty much all floating point operations need to be handled by an external tool like bc or awk. How can I calculate with floating point numbers instead of just integers? |
Quote:
|
Re
Code:
var1=5 Code:
var1=5 |
All times are GMT -5. The time now is 07:43 PM. |