Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
"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
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?
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.
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.
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:
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".
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.