LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-06-2015, 11:36 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
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 01:46 AM. Reason: spelling
 
Old 10-06-2015, 11:46 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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 02:37 AM. Reason: Added bc, CVAR to example
 
2 members found this post helpful.
Old 10-07-2015, 02:54 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 09:55 PM   #4
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
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, 12:49 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 09:55 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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, 11:00 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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 09:44 AM. Reason: *bash* version 4+
 
1 members found this post helpful.
Old 10-09-2015, 08:34 AM   #8
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
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 07:06 PM. Reason: clarity
 
Old 10-09-2015, 09:43 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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



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 08: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 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Where are BASH commands stored? Not scripts but what bash exe uses. theKbStockpiler Programming 11 02-23-2011 03:06 PM
[SOLVED] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 11:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:29 AM.

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