Results 1 to 5 of 5
Hello all-
I've created a crazy script that I am so proud of except I can't figure out a simple math problem that is holding up the project.
I have ...
- 12-29-2007 #1Just Joined!
- Join Date
- May 2007
- Posts
- 15
Simple Math Question
Hello all-
I've created a crazy script that I am so proud of except I can't figure out a simple math problem that is holding up the project.
I have a line in my script that produces a number with a decimal. In my case the number is 75.5. I can't get rid of the ".5". How do I round a number up or down?
It shouldn't be a big deal but my script errors out because the shell can't handle numbers with a ".".
How do I convert 75.5 to 75?
The line from my script is:
fsize=$((size / 1000261785))
and $fsize almost always has a decimal.
Any help would be greatly appreciated. Thanks in advance
- 12-29-2007 #2
That line from your script can't be accurate.
Wasn't there supposed to be a $ before the "size" in that line?
And just to make it easier for us to help you, can you post a tiny, complete script which misbehaves? Something which we can copy and paste into a window and experiment with for you?
Just a suggestion.--
Bill
Old age and treachery will overcome youth and skill.
- 12-29-2007 #3Just Joined!
- Join Date
- Dec 2007
- Posts
- 5
- 12-29-2007 #4Just Joined!
- Join Date
- May 2007
- Posts
- 15
I found a simple solution that met my needs:
Thanks for the input - I see a few new commands I need to check out.Code:#!/bin/tcsh -f read v echo $v/1 | bc exit 0
-= robbie =-
- 12-29-2007 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
We first note:
As an example:ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain
circumstances (see the let builtin command and Arithmetic Expansion).
Evaluation is done in fixed-width integers ...
** exponentiation
* / % multiplication, division, remainder
+ - addition, subtraction
...
Shell variables are allowed as operands; parameter expansion is per-
formed before the expression is evaluated. Within an expression, shell
variables may also be referenced by name without using the parameter
expansion syntax.
-- excerpts from man bash under ARITHMETIC EVALUATION
and:
let: let arg [arg ...]
Each ARG is an arithmetic expression to be evaluated. Evaluation
is done in fixed-width integers with no check for overflow
Producing:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate arithmetic in shell. set -o nounset echo debug=":" debug="echo" ## Use local command version for the commands in this demonstration. echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version bash echo x=7 echo " x is $x" echo echo " x/3 is $(( x/3 ))" echo echo " x / 3 is $(( x / 3 ))" echo echo " using \"let\":" let y=x/2 echo " y is $y" exit 0
See also the very valuable collection of explanations and examples in http://www.tldp.org/LDP/abs/html/arithexp.html , the Advanced Bash-Scripting Guide.Code:% ./s1 (Versions displayed with local utility "version") GNU bash 2.05b.0 x is 7 x/3 is 2 x / 3 is 2 using "let": y is 3
The sample above shows no decimal fractions, only truncation, as expected according to man bash.
As Bill suggested, if you are having problems -- such as getting decimals, as in 75.5 -- then please post the smallest script in its entirety that illustrates the problem.
Best wishes ... cheers, drl
---
Standard advice: Do not use csh family for scripting.Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
