Number comparison in ksh on mac with -lt is giving wrong answer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number comparison in ksh on mac with -lt is giving wrong answer
# 1  
Old 06-04-2013
Number comparison in ksh on mac with -lt is giving wrong answer

I am trying to run following script in ksh on darwin 11.4.2:

Code:
freeSpace=2469606195
spaceNeeded=200
[[ $freeSpace -lt $spaceNeeded ]] && echo "no space" || echo "space available"
[[ $freeSpace < $spaceNeeded ]] && echo "no space" || echo "space available"

"-lt" is giving wrong answer as "no space" Whereas '<' works fine. When I change the freespace value to 5469606195, '-lt' works again. So, its not really limit on variable size or something. Behavior is very weird. Any idea, whats going wrong here?.

Last edited by radoulov; 06-04-2013 at 12:02 PM..
# 2  
Old 06-04-2013
< > are redirection in shell, not greater-than less-than.

Testing in bash, -lt works fine, so I believe you are just hitting the limits on integer size in your shell.
# 3  
Old 06-04-2013
Thanks corona. Yes I too saw that it works in bash. I am hitting issue only in ksh and that too only on Mac.
# 4  
Old 06-04-2013
-lt does a numerical comparison, while < does a string comparison. 200 is less than 2469606195 for both comparisons. Try the operators using either A and B, or 2469606195 and 20000000000, and you will see the difference.
# 5  
Old 06-06-2013
Quote:
Originally Posted by sabitha
Thanks corona. Yes I too saw that it works in bash. I am hitting issue only in ksh and that too only on Mac.
It'd be ugly, but one way to do it on shells with 32-bit integers may be zero-padding your numbers to a fixed number of digits, then using string comparison. String comparison should be reliable on digit strings of identical length.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

VxWorks RTC time giving wrong value at random times

I am seeing a scenario where in if the TIMEZONE environment variable value is set to nothing i.e. putenv "TIMEZONE=" the hardware clock is +1 to software clock.Pasted below the results displayed: -> envShow (global environment) 0: TSC_TIME_FROM_RESET=420150.971529 seconds 1:... (0 Replies)
Discussion started by: snehavb
0 Replies

2. Shell Programming and Scripting

If && command giving wrong output

Hi All, I am trying to run a script which will search for 2 strings(stopped,started) in a text file and echo an output depending on below condition -bash-3.2$ cat trial1.txt v ggg f -bash-3.2$ cat trial1.sh VAR9=` grep 'stopped' /tmp/trial1.txt` VAR10=` grep 'started'... (4 Replies)
Discussion started by: srkmish
4 Replies

3. UNIX for Advanced & Expert Users

xbindkeys giving wrong mapping information

Hello, I'm having a problem with xbindkeys giving the wrong mapping information, hence I can't get it work at all when trying new mappings from this machine. From another computer, I have some definitions for xbindkeys (made with xbindkeys-config). These key codes work correctly on this... (0 Replies)
Discussion started by: Narnie
0 Replies

4. Shell Programming and Scripting

tr command giving wrong output

Hi All, i have a file which have many fields delimited by ,(comma) now i have to show only few fields and not all. the sample text file looks like this: TYPE=SERVICEEVENT, TIMESTAMP=05/06/2009 11:01:40 PM, HOST=sppwa634, APPLICATION=ASComp, FUNCTION=LimitsService, SOU... (8 Replies)
Discussion started by: usha rao
8 Replies

5. Shell Programming and Scripting

Sort command giving wrong output

Hi all, I have a problem with sort command. i have a file which looks like this: "file1 1073 java/4 1073 java/180 1073 java/170 1073 java/176 1073 java/167 1073 java/40 1073 java/33 1073 java/136 28988 java/76 28988 java/73 28988 java/48 28988 java/26" and i want to sort... (8 Replies)
Discussion started by: usha rao
8 Replies

6. UNIX for Dummies Questions & Answers

AWK command giving wrong input

Hi all, I have a problem with qwk command. i have to check process status and for that i am using command prstat -mvL 1 1 and it gives me the entire output but when i use this command with awk like this: prstat -mvL 1 1 | awk -F" " '{print $1,$15}' to get first and 15th arguments. ... (3 Replies)
Discussion started by: usha rao
3 Replies

7. AIX

Shared memory giving wrong value

Hi , I am working on AIX 5.3 server.I have small program which stores the from database to a particaular shared memory.But while retreiving the valus from the same shared memory, i am getting wrong values. Please help..... (1 Reply)
Discussion started by: ajaysahoo
1 Replies

8. Shell Programming and Scripting

Script giving wrong results....

hi, I have this script which gives me the result... #! /usr/bin/sh set -x cd /home/managar a=1 while true do if then echo " File log.txt exists in this directory " exit 0 fi echo " File has not arrived yes..." sleep 3 let a=a+1 if then (1 Reply)
Discussion started by: mgirinath
1 Replies

9. UNIX for Dummies Questions & Answers

mailx and awk- didn't find an answer KSH

Hi folks, My input file's content: You are line1 You are line2 You are line3 etc... I need to write a script that for every line, it will send an e-mail and the body will hold the line: Output(The e-mail that will be sent): First e-mail: To: aaa Subject: bbb Body:You are line1... (4 Replies)
Discussion started by: hellsd
4 Replies
Login or Register to Ask a Question