Sponsored Content
Top Forums Shell Programming and Scripting Non-integer calculations in bash Post 302123893 by jeriryan87 on Wednesday 27th of June 2007 04:09:04 PM
Old 06-27-2007
Non-integer calculations in bash

I'm new at scripting but I thought I was getting pretty good at it. I've hit a snag. I try to use expr to compute a fraction say: expr 3 / 4, and I'm getting zero. I guess it's just truncating to the integer, in this case 0, but I need the decimal 0.75. What can I do to compute this value in order to assign it to a variable?
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Time Calculations

I'm trying to have a loop print out statistics every X number of seconds. How can I add a specific number of seconds to a time variable and make a comparison? Thanks ahead of time. For example: startTime = `date +%H%M%S` currentTime = $startTime executeTime = startTime + X # X is equal... (5 Replies)
Discussion started by: Nysif Steve
5 Replies

2. Shell Programming and Scripting

any way to speed up calculations in bash script

hi i have a script that is taking the difference of multiple columns in a file from a value from a single row..so far i have a loop to do that.. all the data is floating point..fin has the difference between array1 and array2..array1 has 700 x 300= 210000 values and array2 has 700 values.. ... (11 Replies)
Discussion started by: npatwardhan
11 Replies

3. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

4. Shell Programming and Scripting

bash integer & string issue

Hi guys, I need for the bash code below a little bit help: cat script.sh #!/bin/bash 5_MYVALUE="test" echo "$5_MYVALUE" If I try to run the script, got follow failure: ./script.sh ./script.sh: line 4: 5_MYVALUE=test: command not found _MYVALUE My questions are how... (4 Replies)
Discussion started by: research3
4 Replies

5. Shell Programming and Scripting

how to convert string to an integer and how to do calculations like add.,sub.,mult. on it

How to convert string into an integer or number For example : % set tim = `date` % echo $tim Tue Feb 22 16:25:08 IST 2011 here How to increment time by 10 hrs like 16+10 , here 16 is a string in date cmd. .. how to convert 16 to an integer and added to a another nimber ? Thanks... (3 Replies)
Discussion started by: sbhamidi
3 Replies

6. Shell Programming and Scripting

bash script range calculations

Hi, I have data in the following form: AB001 10 AB002 9 AB003 9 etc AB200 5 What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure... (3 Replies)
Discussion started by: chrissycc
3 Replies

7. UNIX for Dummies Questions & Answers

compare decimal and integer values in if in bash shell

i need to do camparisions like the below. For the case when first=10 and second=9.9 the scripts displays process failed. I need to be able to convert the values to integer before doing the comparision. Like 9.9 should be rounded over to 10 before doing comparision. Please advice how can... (3 Replies)
Discussion started by: nehagupta
3 Replies

8. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

9. Shell Programming and Scripting

Arithmetic calculations in bash file

I have 2 numbers xmin = 0.369000018 xmax = 0.569000006 and want to calculate (xmax- xmin) / 5.0 I have tried using $(( )) but is always giving an error (8 Replies)
Discussion started by: kristinu
8 Replies

10. Shell Programming and Scripting

Getting error in bash script; expr $a + 1: integer expression expected

Hi, I am new to shell/bash script. I am trying to run below script #!/bin/bash a=0 b=10 if then echo "a is equal to be" else echo "a is not equal to be" fi MAX=10 while do echo $a a='expr $a + 1' done (1 Reply)
Discussion started by: Mallikgm
1 Replies
EV(1)							      General Commands Manual							     EV(1)

NAME
       ev - evaluate expressions

SYNOPSIS
       ev 'expr' ..

DESCRIPTION
       Ev  evaluates  expressions  given  on the command line, and sends the results to the standard output, one per line.  An expression contains
       real numbers, function calls, and the following operators:

	    +  -  *  /	^

       Operators are evaluated left to right, except '^', which is right associative.  Powers have  the  highest  precedence;  multiplication  and
       division  are  evaluated  before  addition  and subtraction.  Expressions can be grouped with parentheses.  All values are double precision
       real.

       The following library of functions is available:

       if(cond, then, else)
		 if cond is greater than zero, then is evaluated, otherwise else is evaluated.

       select(N, a1, a2, ..)
		 return aN (N is rounded to the nearest integer).  If N is zero, the number of available arguments is returned.

       rand(x)	 compute a random number between 0 and 1 based on x.

       floor(x)  return largest integer not greater than x.

       ceil(x)	 return smallest integer not less than x.

       sqrt(x)	 return square root of x.

       exp(x)	 compute e to the power of x (e approx = 2.718281828).

       log(x)	 compute the logarithm of x to the base e.

       log10(x)  compute the logarithm of x to the base 10.

       sin(x), cos(x), tan(x)
		 trigonometric functions.

       asin(x), acos(x), atan(x)
		 inverse trigonometric functions.

       atan2(y, x)
		 inverse tangent of y/x (range -pi to pi).

EXAMPLE
       To pass the square root of two and the sine of .5 to a program:

	 program `ev 'sqrt(2)' 'sin(.5)'`

AUTHOR
       Greg Ward

SEE ALSO
       icalc(1), rcalc(1)

RADIANCE							     10/28/96								     EV(1)
All times are GMT -4. The time now is 05:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy