LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-04-2009, 06:06 AM   #1
ravidangaych
LQ Newbie
 
Registered: Apr 2009
Posts: 27

Rep: Reputation: 15
Related to shell scripting


how we can solve these arithmatic operations in shell scripting without using bc command

34.23 + 56.98

sin 3.14 (trignometric functions)

how we can take out log values


Could you please tell me some tips for this
 
Old 11-04-2009, 06:33 AM   #2
cantab
Member
 
Registered: Oct 2009
Location: England
Distribution: Kubuntu, Ubuntu, Debian, Proxmox.
Posts: 553

Rep: Reputation: 115Reputation: 115
As far as I know you can't. Bash only does basic integer arithmetic. You will have to use another program. That's what Bash scripting is FOR, automating the execution of other programs.
 
Old 11-04-2009, 07:23 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
why haven't you heard of (g)awk yet?
Code:
$ awk 'BEGIN{print 34.23 + 56.98}'
91.21
$ awk 'BEGIN{print sin(3.14)}'
0.00159265
$ awk 'BEGIN{print log(1)}'
0
 
Old 11-04-2009, 07:33 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Python has good math routines:
Code:
[mherring@Ath play]$ python
<snip>
>>> import math
>>> math.sin(3.14)
0.0015926529164868282
>>> math.log(45)
3.8066624897703196
>>> math.log(100)
4.6051701859880918
>>> math.log10(100)  ##Ah--"log" means natural log##
2.0
>>>
 
Old 11-04-2009, 09:37 AM   #5
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
All math is basic math

"sin()" is nothing more than a function that can calculate predetermined parameters a certain way. Assuming you know what you are doing, you could write a bash script for each of the functions you needed and include them... This would be a major waste of time... but you could.

I suppose if you also wanted to, you could do the same thing anyway using an external program wrapped with a bash function O.o. Depending on how much math you do in your bash scripts.

Chances are though, if you have to do complicated math, you should be using python at least then move up to C/C++ as needed.

Bash should really only be used for the automated execution of programs to complete a certain task. Generally something with very little user input. If it does require user input, it's a config file and the script it self runs without user interaction.

if you are looking for a command line calculator, it's not a large leap to make a menu shortcut that launches an xterm with python automatically importing the math functions you need.

Additionally, if you don't want to type 'math' dot something every time for python, you can import the functions directly
Code:
from math import *
# or individually
# from math import sin log

Last edited by lumak; 11-04-2009 at 09:42 AM.
 
Old 11-04-2009, 10:10 AM   #6
voyciz
Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 425

Rep: Reputation: 40
I hope this isn't considered "threadjacking", I apologize if it is...

How do you get python and bash to communicate? Would you have to use python's os.system to export an environment variable or write to a file? Or is there a way to do it directly?

For instance, if you were adding two numbers, and I have "683.34 + 3532.824" in a python script, how do I get it to "tell" bash the sum?
 
Old 11-04-2009, 10:11 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
As said, bash has almost no math support. Yeah, you could do it, it would involve parsing the strings and doing lots of funny things. Bash will only let you operate with integers. It's not the faster when it comes to iteration either, so integrals or trigonometric functions would take quite a lot of time to compute I guess (I really never tried this, this is neither fun nor good nor even illustrative).

If you truly want to do this in bash, use bc, awk or any other helper as people suggested above.

There might be other shells with better math support, but really... shells are not supposed to do this task. They are just a convenient way to parse commands with very good batching capabilities. If we integrate maths into shells we could very well also integrate every single tool under the sun, which is quite the opposite of the *nix tradition: one task -> one tool, and a good tool that can be reused and assembled with the rest.
 
Old 11-04-2009, 10:25 AM   #8
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by lumak View Post
Chances are though, if you have to do complicated math, you should be using python at least then move up to C/C++ as needed.
I'd recommend Fortran (90 or 95) instead of C/C++ in the case of mathematics, because it's more geared towards that stuff. More mathematical functions, for example, are readily available that you must #include in a C/C++ program, and doing math in Fortran is in my experience quicker (as in writing the program; the actual speed of calculations depends heavily on compiler and optimizations), which is nice. The newer versions seem to become more and more "C++-like", but there's still a difference. C++ is a good language, but it's not the best in everything.

But reading the original post, it seems that the task wasn't (correct me if I'm wrong) to calculate this and that or create a "desktop calculator", but to do a few simple things that might well be a homework assignment in some schools. In this case shell scripts would be the only way to go.
 
Old 11-04-2009, 10:26 AM   #9
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by voyciz View Post
I hope this isn't considered "threadjacking", I apologize if it is...

How do you get python and bash to communicate? Would you have to use python's os.system to export an environment variable or write to a file? Or is there a way to do it directly?

For instance, if you were adding two numbers, and I have "683.34 + 3532.824" in a python script, how do I get it to "tell" bash the sum?
I've never done it (python is not really my language), but I guess something like this should work:

Code:
$ python -i << EOF > foo
import math
math.sin(3.14)
exit()
EOF
Using python for this is just an overkill, though. First, python is not everywhere, bc probably isn't either but I'd consider it "more standard", same goes for "awk".
 
Old 11-05-2009, 04:04 AM   #10
ravidangaych
LQ Newbie
 
Registered: Apr 2009
Posts: 27

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
why haven't you heard of (g)awk yet?
Code:
$ awk 'BEGIN{print 34.23 + 56.98}'
91.21
$ awk 'BEGIN{print sin(3.14)}'
0.00159265
$ awk 'BEGIN{print log(1)}'
0
Thank you
 
Old 11-05-2009, 04:53 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by i92guboj View Post
Using python for this is just an overkill,
No its not. Its just a tool to do stuffs, that's all. same as awk or Perl

Quote:
though. First, python is not everywhere, bc probably isn't either but I'd consider it "more standard", same goes for "awk".
the "Python is not everywhere" argument is based on whether it comes preinstalled. nowadays, its not a problem because if its not preinstalled, then install it. Or build it, its open source anyway. Just because it doesn't come preinstalled for some systems is not a cause for concern when the benefits of using it far outweigh the cost of not using it.
 
Old 11-05-2009, 06:14 AM   #12
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by ghostdog74 View Post
No its not. Its just a tool to do stuffs, that's all. same as awk or Perl
Python and perl are general purpose languages. I'd rather program the whole stuff in python instead of doing a bash/python crossing, but that's just my way, it doesn't mean it has to be yours as well. If you want to call python from bash you are free to do so as well.

You can't compare python, which is a general purpose programming language, to bc which is a concrete tool for a concrete purpose. Of course you can do maths with python, you could use it to list directories instead of 'ls' as well, but in my view, it would be an overkill unless your script is written in python. Loading the interpreter will take much more ram than bc would do. Would be like linking to gecko when all you need is to download a file: better use curl or something like that, no?

Quote:
the "Python is not everywhere" argument is based on whether it comes preinstalled. nowadays, its not a problem because if its not preinstalled, then install it. Or build it, its open source anyway.
The difference is that installing pythong, gcc, perl or java to run a simple bash script is in no one's agenda, while bc and awk is already installed most of the times. And if not, 100% of the people will rather install bc than python to run a shell script.

Again, if you want python, go for it. After all, you are the author and the script is going to be used by you, mainly. So do whatever you want. (it's a generic 'you' aimed at no one in particular).

Quote:
Just because it doesn't come preinstalled for some systems is not a cause for concern when the benefits of using it far outweigh the cost of not using it.
Please, contextualize. *In this thread, what benefits are you talking of when it's all about simple maths? Sure python can fly a jumbo, but we don't need to fly a jumbo now, we need to sum numbers and the like. Sure you can kill a fly with a a-bomb, but I'd rather use something smaller.

Last edited by i92guboj; 11-05-2009 at 06:19 AM.
 
Old 11-05-2009, 06:21 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by i92guboj View Post
but that's just my way, it doesn't mean it has to be yours as well. If you want to call python from bash you are free to do so as well.
i would do it with Python as well, if its for myself.

Quote:
You can't compare python, which is a general purpose programming language, to bc which is a concrete tool for a concrete purpose. Of course you can do maths with python, you could use it to list directories instead of 'ls' as well, but in my view, it would be an overkill unless your script is written in python.
overkill to me, means doing the unnecessary. In the above case, if I have to write plenty of code just to implement sin() for example, then its overkill.
 
Old 11-05-2009, 08:36 AM   #14
voyciz
Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 425

Rep: Reputation: 40
Ah, I see a way to do it now. If you do something like:

$ script.sh $(pyscript.py 34.23 56.98)

Then if the python script was setup to handle it right, it would be passed as an arg to a bash script. So simple I, I love it. This will help me in one of my projects.
 
Old 11-05-2009, 08:47 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by voyciz View Post
Ah, I see a way to do it now. If you do something like:

$ script.sh $(pyscript.py 34.23 56.98)

Then if the python script was setup to handle it right, it would be passed as an arg to a bash script. So simple I, I love it. This will help me in one of my projects.
if you can use Python, then do everything in Python.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting question, homework related cybergeek11235 Linux - General 4 10-12-2008 10:59 PM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Need help in some scripting related to router information gathering. kharerohit Linux - Networking 1 03-17-2006 10:22 AM
Help in scripting related to router information gathering. kharerohit Linux - Networking 1 03-17-2006 05:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:12 PM.

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
Open Source Consulting | Domain Registration