LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Arithmatic calculation in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/arithmatic-calculation-in-shell-script-4175432684/)

shivaa 10-17-2012 08:01 AM

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?

Habitual 10-17-2012 08:04 AM

maybe
Code:

let x=5
let y=4

?

Edit: I vote "no" on "let..."
Sorry about that.

Snark1994 10-17-2012 08:08 AM

Code:

#!/usr/bin/env bash
x=4
y=5
ans=$((x + y))
echo $ans

prints 9 for me, with

Code:

$ bash --version
GNU bash, version 4.2.37(2)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Can you post the exact code you're using? I wouldn't expect the 'ans=$ unexpected' error unless there was something odd on the line before.

Note: alternatively, you could use

Code:

echo '4+5' | bc
but that's probably overkill given bash has such functionality builtin.

shivaa 10-17-2012 09:35 AM

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.

Snark1994 10-17-2012 09:43 AM

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 :)

shivaa 10-17-2012 11:58 AM

Quote:

Originally Posted by Snark1994 (Post 4808190)
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 :)

I am sorry that I marked it as solved, but re-opening it. It is running ok when I am using this script in csh or tcsh. But same isn't at all working with /bin/sh or bash. Perhaps bash has some limitations over c shell.
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?

shivaa 10-17-2012 12:07 PM

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?

ButterflyMelissa 10-17-2012 12:48 PM

hey!

Consider bc, an example:

Quote:

bc -il
then enter your equation, you should get the required result...

Thor

valdinei 10-17-2012 12:58 PM

Check This result

Code:

root@ubuntu-vm:~# echo 5*20/3 | bc -l
33.33333333333333333333


bigrigdriver 10-17-2012 01:40 PM

Code:

echo "scale=3; (5*20/3)" | bc -l
33.333

The bc scale function sets the number of decimal places in the answer.

shivaa 10-17-2012 10:56 PM

Quote:

Originally Posted by bigrigdriver (Post 4808392)
Code:

echo "scale=3; (5*20/3)" | bc -l
33.333

The bc scale function sets the number of decimal places in the answer.

OK. Everything works until we use numbers only, but nothing works when we pass any variable in this command.
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/

shivaa 10-17-2012 11:32 PM

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!!

David the H. 10-20-2012 10:01 AM

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?

Snark1994 10-21-2012 04:52 AM

Quote:

Originally Posted by David the H. (Post 4810736)
I'm kind of surprised nobody has specifically pointed this out yet: bash arithmetic is currently integer only.

That would be 'cos I had no idea... Good job we've got you around, then!

chrism01 10-21-2012 06:30 PM

Re
Code:

var1=5
var2=20
and echo 100*var1/var2 | bc -l

You need to prefix vars with $ when reading them eg
Code:

var1=5
var2=20
echo 100*var1/var2 | bc -l

# output
25.00000000000000000000



All times are GMT -5. The time now is 07:43 PM.