Dividing float values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Dividing float values
# 1  
Old 01-16-2007
Dividing float values

Hi

I know its a dumb question but can any one please explain me the difference of executing a shell script in the following 2 ways.

. script.sh
sh script.sh

I have a problem if I execute the following code as sh script.sh

DB_CNT_ALW=0.20
SCT_VAR=0.05

if [ "$(echo "if (${DB_CNT_ALW} > ${SCT_VAR}) 1" | bc)" -eq 1 ]; then
echo "== Difference is greater"
else
echo "== Difference is not greater"
fi

The above mentioned code was suggested by vgersh99. But the same code works well if I execute the script as . script.sh
I would prefer executing the script as sh script.sh since if I use exit command inside the script it won't come out of unix but instead exit from the script.

Thanks
Shash
# 2  
Old 01-16-2007
". script.sh" will execute the script in the current environment, as if you had typed all the commands in script.sh at the command prompt. Therefore, an "exit" in the script will exit your session. Why not just chmod +x the script and run it by doing:
Code:
./script.sh

?
# 3  
Old 01-16-2007
You can't run that script as:
sh script.sh
because sh is an ancient shell that cannot run that code. You need to use
ksh script.sh
bash script.sh
or some other reasonably modern shell.
# 4  
Old 01-17-2007
Thanks Glenn Arndt & Perderabo!!

Both the solutions are working.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dividing values within rows with awk?

Hello everyone, I have a dataset that looks like: 1 2 3 4 5 6 I was wondering if was possible to divide values within rows: 1/2 3/4 5/6 I don't think using awk -F"\t" '{ print $1/$2 }' would work here, or would it? Thanks in advance! (2 Replies)
Discussion started by: Rabu
2 Replies

2. Shell Programming and Scripting

Dividing by zero

Does anyone know how to include as a script maybe an "echo" warning that explains that if a user uses the second number "zero" when dividing, that the result will BE "zero." I need, example: 5/0 (second number) = 0, in script form. current script: echo "Enter a number" read num1 echo... (4 Replies)
Discussion started by: jefferj54
4 Replies

3. Programming

Printing float values in C

Hi, I have small problem to print float value in the fallowing code float Cx, W,f=250000, Cr=92.00,pi=3.14; W=2*pi*f; Cx = 1/W.Cr; //Cx value will be come around like 7.07E-9. printf("capacitance value: %.10f",Cx); I am trying to print Cx value using above code but it was not... (3 Replies)
Discussion started by: veerubiji
3 Replies

4. Shell Programming and Scripting

Issues when dividing

Hi, I do have a very simple task to divide 2 variables and display the result. I CANNOT use bc when i try var1=2 var2=4 var3=$(($var1 / $var2)) echo $var3 the output is always 0 What can I change to get a dotted decimal result such as 0.5 ? Thanks! (5 Replies)
Discussion started by: svetoslav_sj
5 Replies

5. UNIX for Dummies Questions & Answers

Comparing two files and dividing the values

Hi all, I am new to unix and I am trying hard to get this requirement, but no luck. I am trying to compare two cloumns in two files and if it matches, the last column in file1 must be divided by file2 and the output must be written in a new file. To elaborate the 2nd column in file1 (EUR) must be... (6 Replies)
Discussion started by: smadderla
6 Replies

6. UNIX for Advanced & Expert Users

Get Minimun from a Float values

Hi Guys, here is a part of my source code. This part is reponsible to get the minimun of a few values: .......... MAX=0.00 for a in `cat $OFILE` do if then ... (2 Replies)
Discussion started by: Paat
2 Replies

7. Shell Programming and Scripting

SH if statement using FLOAT values

Today I spent longer than I'd like to admit figuring out how to write a Bourne shell IF statement that tests a FLOAT value before executing a block of statements. Here's the solution I found, which invokes bc. Hope this will come in handy for someone: value = testval = if then body... (5 Replies)
Discussion started by: sjepsen
5 Replies

8. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

9. Shell Programming and Scripting

comparing two float values

I am trying to compare 2 float values using if the foll code does not work a=1.4 b=1.6 if test $a -gt $b then echo "$a is max" else echo "$b is max" fi does -gt work for floating point numbers, if not how do go about for my requirement? can i use bc ? pls help thanks in advance... (2 Replies)
Discussion started by: kavitha
2 Replies

10. UNIX for Dummies Questions & Answers

dividing the screen >>>

Hi! Is there is any way to divide the screen when I use UNIX shells (I have RedHat 7.1)? I have to see the command promt and process some data from very long file at the same time (I work with PThreads). Some "pseudo-windows" in text mode, huh? =) Thanks in advance and don't be angry =) (4 Replies)
Discussion started by: ShockTeam
4 Replies
Login or Register to Ask a Question