floating point variable in UNIX script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting floating point variable in UNIX script
# 1  
Old 02-01-2006
floating point variable in UNIX script

HI all,

Can someone help me how to use or assign a floating point value in a variable in a unix script. I dont know how to use it.

for example, I am to assign a variable VAR1 a value of 2.34 and evaluate if a certain vaue is greater or less than to it.

That is,

if var1 > 2.13 will do something...

Please help me. I would appreciate your help.

Thanks
Darry
# 2  
Old 02-01-2006
Code:
#!/bin/ksh

a=1.72
b=1.71

if [ "$(echo "if (${a} > ${b}) 1" | bc)" -eq 1 ] ; then
   echo ">"
else
   echo "<"
fi;

# 3  
Old 02-01-2006
=>var1=2.13
=>if [ $var1 \< 3 ]
> then
> echo hi
> else
> echo by
> fi

This works for me, the > and < are redirection operators, so just escape them by the \
# 4  
Old 02-01-2006
Quote:
Originally Posted by linuxpenguin
This works for me, \
Will only work with bash, though.
# 5  
Old 02-02-2006
yeah it will work only in bash

use awk in that case

echo $var1|awk ' {if ($0 < 3 ) { print "hi" } else { print "by" } }'
# 6  
Old 02-08-2006
working with bc

Quote:
Originally Posted by vgersh99
Code:
#!/bin/ksh

a=1.72
b=1.71

if [ "$(echo "if (${a} > ${b}) 1" | bc)" -eq 1 ] ; then
   echo ">"
else
   echo "<"
fi;


Hi vgersh99,

Can you please tell me how to use the else clause in the if statement you have written. (i.e. "if (${a} > ${b}) 1" | bc)" )

so that i will have something like this
"if (${a} > ${b}) 1 else 0" | bc)"
But as it is this is not working in ksh.
# 7  
Old 02-08-2006
Quote:
Originally Posted by rony_daniel
Hi vgersh99,

Can you please tell me how to use the else clause in the if statement you have written. (i.e. "if (${a} > ${b}) 1" | bc)" )

so that i will have something like this
"if (${a} > ${b}) 1 else 0" | bc)"
But as it is this is not working in ksh.
could you elaborate on what you've tried and what exact is not working?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. Shell Programming and Scripting

IF statement on floating point variable in scientific format

Hi all, I'm struggling :wall: to make a an if statement on two variables in scientific format. e.g. a=1.23e+01 b=3.21e+02 I know that it can be done with bc if the variable are not in scientific format. But what to do in this case??? Thank you, (3 Replies)
Discussion started by: f_o_555
3 Replies

3. UNIX for Advanced & Expert Users

Floating point argument for sleep command in Unix

Hi , How can I use floating point argument for sleep command in unix.I cannot use usleep as it is not suppported. Also how can I find out that a file is in use at that particular instant only.(wether it is being read or written) (7 Replies)
Discussion started by: kanus
7 Replies

4. Shell Programming and Scripting

Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers. The length of the number should be 13 including decimal. The input number is changing so number of leading zeros is not fix. For example input output 216.000 000000216.000 1345.000 000001345.000 22345.500 ... (4 Replies)
Discussion started by: reeta_shri
4 Replies

5. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

6. Shell Programming and Scripting

Floating point to integer in variable length lines

Hi ! I'm looking for a way to transform certain floating point numbers in a one-line, variable length file to integers. I can do this in a crude way with sed : sed -e 's/0\.\(\):/\1:/g' -e 's/0\.0\(\):/\1:/g' -e 's/1\.000:/100:/g' myfile ... but this doesn't handle the rounding correctly. ... (3 Replies)
Discussion started by: jossojjos
3 Replies

7. Shell Programming and Scripting

How to compare floating point numbers in shell script?

How can we compare 2 floating point numbers in SHELL script? (11 Replies)
Discussion started by: dearanik
11 Replies

8. Linux

Floating point exception !!!

Hi, I have linux fedora 4 ver., 2.6 kernal. And qmail & mysql & samba servers are already configured on this server. When I try to install any package like squidguard ,dansguardian,webmin,rsnapshots with command rpm -ivh . It is giving error as “Floating point exception" Snap View is... (3 Replies)
Discussion started by: ssk01
3 Replies

9. Shell Programming and Scripting

floating point shell variable

I have a two files >cat file1 jjjjj 10.345 6.673 ppp 9.000 5.883 >cat file2 mmm 80 10 jjjjj 10.305 6.873 ppp 9.000 5.883 I am reading file 1 line by line , and look for the string jjjj and then read the line in file 2 with jjjj I want to get the... (5 Replies)
Discussion started by: jojan
5 Replies

10. Shell Programming and Scripting

Floating Point Division

Does anyone have a simple way of doing floating point ("fp") division? For example, if I divide 3 by 5, I can get 0.6. The built-in calc (`bc`) will perform fp multiplication, but not division, at least not straight-up (i.e., starting bc and just typing in 3/5). I am trying to do this using... (1 Reply)
Discussion started by: gsatch
1 Replies
Login or Register to Ask a Question