Visit Our UNIX and Linux User Community


check if a decimal number is greater than zero


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers check if a decimal number is greater than zero
# 1  
Old 09-21-2011
Question check if a decimal number is greater than zero

Hello,

In my code I am checking to see if a variable that contains a decimal number is greater than 0 in the following manner:

if [ $i -gt 0 ]
do something
fi


However I am getting the error message (if $i for the current iteration holds 9.6352)

command 9.6352 is not found

How can I rectify this problem?

Thanks,
Guss.
# 2  
Old 09-21-2011
Unless you're using a really new ksh, numeric comparison doesn't work on numbers with decimal points.

Code:
if echo "$i" | awk '($1 <= 0) { exit 1 }'
then
...
fi

This is most definitely not an efficient way to do it! If you have ksh available, try it. Otherwise, better ways might involve transforming the number into something without decimal places so the shell can handle it natively. Perhaps if I knew where this number was coming from and what you were using it for...
# 3  
Old 09-21-2011
see the example below:



Code:
kent$  a=9.6532
kent$  [ $a -gt 0 ] && echo "ok" || echo "wrong"                                                                                         
[: integer expression expected: 9.6532
wrong

kent$  [ ${a%%.*} -gt 0 ] && echo "ok" || echo "wrong"
ok

# 4  
Old 09-21-2011
Quote:
[ ${a%%.*} -gt 0 ] && echo "ok" || echo "wrong"
This test will fail where a < 1.

Quote:
Unless you're using a really new ksh, numeric comparison doesn't work on numbers with decimal points.
Huh? Floating point support, what you call numbers with decimal points, has been available in ksh93 since 1993.

If you want to do floating point arithmetic from within a bash script, you need to use an external utility such as bc. See Floating Point Math in Bash
# 5  
Old 09-22-2011
Hi,
I am not sure about the ksh version thing, but it works in my system
Code:
#! /usr/bin/ksh
a=9.2
  if [ $a > 0 ]
    then
   echo "Correct"
 else
   echo "incorrect"
 fi

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Franklin52; 09-22-2011 at 05:15 AM..
# 6  
Old 09-22-2011
Quote:
Originally Posted by fpmurphy
Huh? Floating point support, what you call numbers with decimal points, has been available in ksh93 since 1993.
And it'd be nice if more people had that. Unless specifically told otherwise it's safest to assume ksh88.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #183
Difficulty: Easy
Over 40% of UNIX.com users voted that bash was their most useful shell.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Greater than specific number

please let me know how to construct if then else by comparing two numbers if it is greater than 10000. I need to do some specific task executed. can you help me out in shell scripting plz. (6 Replies)
Discussion started by: ramkumar15
6 Replies

2. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

3. Shell Programming and Scripting

Egrep a number greater than in a column

i'm aware awk can do what i'm trying to do here. but i cant use awk in this scenario given the circumstance of this box. but i need to check if a number is a certain column is over a certain value, say for instance, 20. data: | 12 | 19 | 2000 | 9029333 |... (11 Replies)
Discussion started by: SkySmart
11 Replies

4. Shell Programming and Scripting

Egrep a greater than number

data: hello mr smith 400 you all ok? hello mrs. smith 700 you all ok? hello mr. everyone 150 you all ok? hello mr. you all 199 im lad you are ok using egrep, how can i grep out only lines that have a number greater than 250? cat data | egrep ..... can't use awk here. i was... (7 Replies)
Discussion started by: SkySmart
7 Replies

5. Shell Programming and Scripting

Need to check the value greater than or less than and give out put to a file

HI, I have one file which is as below cat /var/tmp/test1 | awk '{ print $3}'|grep -v affected Data ---------- 200.4 . The above 200 value is changable by the database script. Now I need a script that checks the value 200.4 and the script shoud give out put if value is more than 225 (2 Replies)
Discussion started by: phani4u
2 Replies

6. Shell Programming and Scripting

AWK: Cannot read Number of records greater than 1(NR>1)

Hi all, I have a tab-delimited text file of size 10Mb. I am trying to count the number of lines using, grep -c . sample.txtor wc -l < sample.txt or awk 'END {print NR}' sample.txtAll these commands shows the count as 1, which means they are reading only the first header line of the file.... (3 Replies)
Discussion started by: mehar
3 Replies

7. Shell Programming and Scripting

[Solved] Select the columns which have value greater than particular number

i have a file of the form 9488 14392 1 1.8586e-07 5702 7729 1 1.8586e-07 9048 14018 1 1.8586e-07 5992 12556 1 1.8586e-07 9488 14393 1 1.8586e-07 9048 14019 1 1.8586e-07 5992 12557 1 1.8586e-07 9488 14394 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

8. Shell Programming and Scripting

Check numeric fields greater than zero, and delete lines if appropriate

This be the latest in my problems sorting through router logs... I'm half way there on a problem, but I've hit the limitation of my knowledge Got some router interface log files of type router01:GigabitEthernet9/24 is up, line protocol is up (connected) router01: 0 input errors, 0 CRC, 0... (7 Replies)
Discussion started by: Yorkie99
7 Replies

9. Shell Programming and Scripting

Show result only if number is greater then

Hello all Im trying to write one liner that will show me results only if the result of the expression is greater then 0 For example: I do : find . -name "*.dsp" | xargs grep -c SecurityHandler the result are : ./foo/blah/a.dsp:0 ./foo/blah1/b.dsp:1 ./foo/blah2/c.dsp:2... (1 Reply)
Discussion started by: umen
1 Replies

Featured Tech Videos