[SOLVED] bash bc divide script_Any more simple equivalents?
Linux - NewbieThis 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
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.
Is there anyway to separate the "echo" and the "results = " into separate lines? Is the above solution really the simpliest way of writing this question? Any way of breaking down the above line into several shorter/simplier lines?
Many noob bash learners like I experience lower retention/slower learning rates when using compound/complex statements from the start. However, when we can separate different ideas onto each line, than we learn quicker/higher retention rates.
#BACKUP INFO:
#1) I also read the tutorial on http://www.novell.com/coolsolutions/tools/17043.html, but #this solution merely solves the problem by changing your single computers' bash settings,
#thus is not cross platform.
#2)Entire script:
Code:
#!/bin/bash
unset num1 num2 result
#MAIN PROG
# Obtain user input
echo -e "Please enter two numbers \c"
read num1 num2
# Arithmetic
# set -vx
result=$(echo "scale=2; $num1/$num2"|bc);
echo -e "Result is: $result"
# set +vx
Last edited by andrew.comly; 10-07-2015 at 01:46 AM.
Reason: spelling
What you probably want are the rules for ARITHMETIC EXPANSION and ARITHMETIC EVALUATION from the bash man page.
In particular, you need to see the syntax for the let statement, and its equivalent ((...)).
To get you going, start here in man bash:
Code:
Compound Commands
A compound command is one of the following:
...
((expression))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION.
If the value of the expression is non-zero, the return status is 0; otherwise the return status
is 1. This is exactly equivalent to let "expression".
Now, bash only produces integer results, so if you really need the floating point result bc is still the way to go, but maybe with a simpler evaluation.
As per astrogeek's examples, shell built-ins only do integer calcns, which means if you want non-int answers eg during division, then you'll need an external cmd like bc.
Here's a very good page http://www.basicallytech.com/blog/ar...ions-using-bc/
Excellent tutorial. At first I thought the echo looked rather superfluous, but after reading the above tutorial, I am now accustomed to seeing/writing an echo in each line. I guess the statement
I think its due to the fact that bc is in fact an interactive tool (page down a bit) so you have to 'cheat' a bit to get non-interactive usage as well.
Is there anyway to separate the "echo" and the "results = " into separate lines? Is the above solution really the simpliest way of writing this question? Any way of breaking down the above line into several shorter/simplier lines?
You can use Coprocesses (requires bash version 4+) to break things down. This means you have to explicitly manage the pipes using more advanced Redirection syntax so you may not consider it simpler:
Code:
coproc bc # start bc
echo "scale=2; $num1/$num2" >&${COPROC[1]} # send data to bc
read result <&${COPROC[0]} # read response from bc
exec {COPROC[1]}>&- # close pipe, causes bc to quit
Last edited by ntubski; 10-09-2015 at 09:44 AM.
Reason: *bash* version 4+
You can use Coprocesses (requires version 4+) to break things down. This means you have to explicitly manage the pipes using more advanced Redirection syntax so you may not consider it simpler:
Code:
coproc bc # start bc
Code:
a@NP-NC110:~$ sudo apt-get install coproc
[sudo] password for a:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package coproc
a@NP-NC110:~$ sudo apt-get install coprocesses
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package coprocesses
How to install that one!??(*)? I guess I don't have version 4+
Last edited by andrew.comly; 10-09-2015 at 07:06 PM.
Reason: clarity
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.