LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-07-2015, 12:36 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Lubuntu 14.04 LTS
Posts: 233
Blog Entries: 2

Rep: Reputation: Disabled
Red face 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

Last edited by andrew.comly; 10-07-2015 at 02:46 AM. Reason: spelling
 
Old 10-07-2015, 12:46 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,879
Blog Entries: 1

Rep: Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998
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"

Last edited by astrogeek; 10-07-2015 at 03:37 AM. Reason: Added bc, CVAR to example
 
2 members found this post helpful.
Old 10-07-2015, 03:54 AM   #3
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
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/
 
1 members found this post helpful.
Old 10-07-2015, 10:55 PM   #4
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Lubuntu 14.04 LTS
Posts: 233
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Thumbs up Nice tutorial suggestion

Quote:
Originally Posted by chrism01 View Post
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.
 
Old 10-08-2015, 01:49 AM   #5
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
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.
 
1 members found this post helpful.
Old 10-08-2015, 10:55 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
You can eliminate the echo command and use a "here string":
Code:
bc <<<"scale=2; $num1/$num2"
 
1 members found this post helpful.
Old 10-08-2015, 12:00 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by andrew.comly View Post
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 10:44 AM. Reason: *bash* version 4+
 
1 members found this post helpful.
Old 10-09-2015, 09:34 AM   #8
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Lubuntu 14.04 LTS
Posts: 233
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Question

Quote:
Originally Posted by ntubski View Post
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 08:06 PM. Reason: clarity
 
Old 10-09-2015, 10:43 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by andrew.comly View Post
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.
 
1 members found this post helpful.
  


Reply

Tags
bash scripting, bc, calculation, simple, variables


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
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 09:08 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 03:40 AM
Where are BASH commands stored? Not scripts but what bash exe uses. theKbStockpiler Programming 11 02-23-2011 04:06 PM
[SOLVED] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 12:42 PM


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