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.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

I want to evaluate a simple math equation using bash, and would like to have returned a floating number with an accuracy to the tenth i.e 5.5

the code I have currently is:

myEXP200=$[(25*($reflength-11+1))/$reflength]

where $reflength are already stored values, I was looking at some code and saw that they piped the math equation to |bc -l , but thats not really what I want.

That could be why . So I looked over it some more and I read up on scale and length and believe I need to use scale but not sure how to do it in the form I want. I am guessing I could do the following:

which didnt work. On the bc man page it says I can do scale ( expression ) but I have yet to find an example using this syntax. I believe that the scale=1 is an environmental element so I should take it out of my equation but what about the bc? Should it be inside or out of the $() ?

which didnt work. On the bc man page it says I can do scale ( expression ) but I have yet to find an example using this syntax. I believe that the scale=1 is an environmental element so I should take it out of my equation but what about the bc? Should it be inside or out of the $() ?

Best,

Weldon

Try:

Code:

myEXP=$(echo "scale=5; (25*($reflength-11+1))/$reflength" | bc -l)

Scale is synonymous with number of decimal points. -l defines the standard maths library; may not be necessary, but I always use it, just in case.

Last edited by pwc101; 09-22-2008 at 10:56 AM.
Reason: clarification

Brilliant. It worked perfectly. I did look at the -l flag but when I used it stand alone with a math function, it just gave me a bunch of zeros after the decimal -- not sure what its set scale is.

Do I have to use the echo? And if so, why? Does it just basically tell the shell to evaulate the statement in " " and save the output as myEXP?

Brilliant. It worked perfectly. I did look at the -l flag but when I used it stand alone with a math function, it just gave me a bunch of zeros after the decimal -- not sure what its set scale is.

Do I have to use the echo? And if so, why? Does it just basically tell the shell to evaulate the statement in " " and save the output as myEXP?

Best,

Weldon

bc is an interactive calculator (type bc on its own in a terminal and you'll get a command line). You can enter the statement in the $() construct, and you'll get an answer printed out on the screen

The $() construct means "evaluate all the commands contained in the brackets". Here, we're assigning that result to the variable myEXP.

So, what we're doing here is sending the information we want evaluated to bc through standard input, but we can't just type it without the echo, or the shell will try and execute the command scale (since it's the first one in the $() construct). Echoing the commands means they get sent to bc for it to read as though they were bc commands.

Hope this helps

Last edited by pwc101; 09-22-2008 at 11:12 AM.
Reason: more clarification

A most excellent description I understand know why you need the echo.

Here's a simple one for you

var1=2
var2=5

var3= var1 + var2

Why does,

var3=$($var1 + $var2)
or
var3=$(($var1 + $var2))
or
var3=$[$var1 + $var2]

not work? I know for bash if you want to evaluate a math function, you give it the form $[] with numbers in the brackets, but what if they are stored variables?

I should also point out that var1 and var2 in my program are decimals, to which I am returned this error from this line:
myTOTEXP=$(($myEXP200 + $myEXP500))

error:
./command_assem.sh: line 68: 24.9 + .9: syntax error in expression (error token is ".9 + .9")

Figured it out, just had to use the same syntax as above (pipe to bc and flag scale). I didn't realise that all floating point numbers had to go through bc

I should also point out that var1 and var2 in my program are decimals, to which I am returned this error from this line:
myTOTEXP=$(($myEXP200 + $myEXP500))

error:
./command_assem.sh: line 68: 24.9 + .9: syntax error in expression (error token is ".9 + .9")

where $myEXP200 is 24.9 and $myEXP500 is .9

The problem here is not that the values are stored in variables (bash has translated them from variables to the variable's content as you can see because the error knows these are numbers), the problem is they are decimals; bash cannot do floating point arithmetic - it can only handle integer values. That's why there's bc.

To see how bash is interpreting your script, it's useful to put "set -x" at the start of the script, after the shebang (#!/bin/bash), and then run your code as normal. You can turn it off by just commenting the set -x line.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.