LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash bc divide script_Any more simple equivalents? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-bc-divide-script_any-more-simple-equivalents-4175555495/)

andrew.comly 10-06-2015 11:36 PM

bash bc divide script_Any more simple equivalents?
 
MAIN QUESTION:
I would like to make a bash script that can divide two integers.

After a lot of failure, I read http://www.linuxquestions.org/questi...ulator-602583/ and finally succeeded with:
Code:

result=$(echo "scale=2; $num1/$num2" |bc);
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


astrogeek 10-06-2015 11:46 PM

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.

Here is a free example to jump-start you:

Code:

#!/bin/bash

AVAR=""
BVAR=""
CVAR=""

let "AVAR=10/5"
((BVAR=20/4))
CVAR=$(bc <<< "scale=2; 17/3")

echo "$AVAR"
echo "$BVAR"
echo "$CVAR"


chrism01 10-07-2015 02:54 AM

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/

andrew.comly 10-07-2015 09:55 PM

Nice tutorial suggestion
 
Quote:

Originally Posted by chrism01 (Post 5431117)

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

result=$(echo "scale=2; $num1/$num2" |bc);
isn't so compound/complicated after all.

chrism01 10-08-2015 12:49 AM

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.

rknichols 10-08-2015 09:55 AM

You can eliminate the echo command and use a "here string":
Code:

bc <<<"scale=2; $num1/$num2"

ntubski 10-08-2015 11:00 AM

Quote:

Originally Posted by andrew.comly (Post 5431066)
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


andrew.comly 10-09-2015 08:34 AM

Quote:

Originally Posted by ntubski (Post 5431771)
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+ :)

ntubski 10-09-2015 09:43 AM

Quote:

Originally Posted by andrew.comly (Post 5432133)
How to install that one!??(*)? I guess I don't have version 4+ :)

Pardon my terseness, I meant bash version 4 or greater has it as a builtin command.


All times are GMT -5. The time now is 06:45 PM.