LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-17-2012, 09:01 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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?

Last edited by shivaa; 10-17-2012 at 09:03 AM.
 
Old 10-17-2012, 09:04 AM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,163
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
maybe
Code:
let x=5
let y=4
?

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

Last edited by Habitual; 10-17-2012 at 09:06 AM.
 
Old 10-17-2012, 09:08 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

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

Last edited by Snark1994; 10-17-2012 at 09:12 AM.
 
Old 10-17-2012, 10:35 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Original Poster
Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 10-17-2012, 10:43 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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
 
Old 10-17-2012, 12:58 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Original Poster
Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by Snark1994 View Post
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?

Last edited by shivaa; 10-17-2012 at 01:00 PM.
 
Old 10-17-2012, 01:07 PM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Original Poster
Rep: Reputation: 286Reputation: 286Reputation: 286
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?
 
Old 10-17-2012, 01:48 PM   #8
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,686
Blog Entries: 23

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
hey!

Consider bc, an example:

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

Thor
 
Old 10-17-2012, 01:58 PM   #9
valdinei
LQ Newbie
 
Registered: Jul 2012
Location: Brazil
Distribution: Ubuntu, Freebsd, CentOS
Posts: 17

Rep: Reputation: Disabled
Check This result

Code:
root@ubuntu-vm:~# echo 5*20/3 | bc -l
33.33333333333333333333
 
Old 10-17-2012, 02:40 PM   #10
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
Code:
echo "scale=3; (5*20/3)" | bc -l 
33.333
The bc scale function sets the number of decimal places in the answer.
 
Old 10-17-2012, 11:56 PM   #11
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Original Poster
Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by bigrigdriver View Post
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/
 
Old 10-18-2012, 12:32 AM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Original Poster
Rep: Reputation: 286Reputation: 286Reputation: 286
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!!
 
Old 10-20-2012, 11:01 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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?
 
1 members found this post helpful.
Old 10-21-2012, 05:52 AM   #14
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by David the H. View Post
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!
 
Old 10-21-2012, 07:30 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

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


Reply

Tags
calculation


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
elapsed time calculation from two date strings - shell script hisunday Linux - General 5 03-29-2012 08:11 AM
little calculation in script. pinga123 Linux - Newbie 19 07-20-2010 07:17 AM
[SOLVED] complex math calculation in shell script needed gary_in_springhill Programming 33 03-20-2010 06:47 AM
shell - acting on result of calculation corck Programming 3 07-05-2005 10:16 AM


All times are GMT -5. The time now is 10:34 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration