The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparing Decimal Numbers Grizzly Shell Programming and Scripting 3 10-05-2009 04:17 AM
Insert a decimal point lyoncc Shell Programming and Scripting 4 06-04-2007 06:40 PM
how to get rid of decimal point? cin2000 Shell Programming and Scripting 2 12-27-2005 12:26 PM
How To Make Decimal Point Fall On The 15th Character On The Screen Vozx Shell Programming and Scripting 3 12-08-2005 04:17 PM
comparing two decimal values in KSH nandinisagar Shell Programming and Scripting 4 11-15-2005 06:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 240
Quote:
Originally Posted by Scrutinizer View Post
Bash I am not surprised since I gather that is not a happy marriage with Solaris (is it even installed on your system?). However the problem with posix compliant version puzzles me. What is the output of this command on your system:

Code:
echo "12.13>12.14"|bc; echo "12.14>12.13"|bc


Code:
Yes I have bash version 3.0 on my system and the error massage when putting 

bash code
syntax error on line 1, teletype
bash: ((: > 0 : syntax error: operand expected (error token is "> 0 ")

posix code
syntax error on line 1, teletype
bash: [: -gt: unary operator expected

  #2 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 750
OK, and:

Code:
echo "12.13 > 12.14"|bc; echo "12.14 > 12.13"|bc

  #3 (permalink)  
Old 4 Weeks Ago
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 240
Quote:
Originally Posted by Scrutinizer View Post
OK, and:

Code:
echo "12.13 > 12.14"|bc; echo "12.14 > 12.13"|bc

just it give me the same error in below and that's it


Code:
syntax error on line 1, teletype
syntax error on line 1, teletype

  #4 (permalink)  
Old 4 Weeks Ago
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Quote:
Originally Posted by ahmad.diab View Post
[...]
SunOS server2 5.10
On Solaris you can also use ksh93 (you may have it under /usr/dt/bin/dtksh), zsh or expr.


Code:
% zsh -c '((1.20 > 1.3)) && print ok || print ko'
ko

or:


Code:
$ bash -c '(( $(expr 1.20 \> 1.3) == 1 )) && echo ok || echo ko' 
ko


Code:
bash -c 'case $(expr 1.20 \> 1.3) in (1) echo ok;; (*) echo ko;; esac'
ko

  #5 (permalink)  
Old 4 Weeks Ago
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by radoulov View Post
Code:
$ bash -c '(( $(expr 1.20 \> 1.3) == 1 )) && echo ok || echo ko' 
ko


Code:
bash -c 'case $(expr 1.20 \> 1.3) in (1) echo ok;; (*) echo ko;; esac'
ko

Like bash, expr only does integer arithmetic; you cannot use it with decimal fractions.

In any POSIX shell, you can strip the fraction and compare the integer portions. If they are equal, then compare the decimals:


Code:
if [ ${1%.*} -gt  ${2%.*} ]
then
  printf "%s is larger than %s\n" "$1" "$2"
elif [ ${1%.*} -eq  ${2%.*} ]
then
  : decimal comparison left as exercise for the reader 
else
  printf "%s is smaller than %s\n" "$1" "$2"
fi

  #6 (permalink)  
Old 3 Weeks Ago
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Quote:
Originally Posted by cfajohnson View Post
Like bash, expr only does integer arithmetic; you cannot use it with decimal fractions.
I had that doubt after posting the example. Thank you for pointing it out.

Quote:
In any POSIX shell, you can strip the fraction and compare the integer portions. If they are equal, then compare the decimals:
Code:
if [ ${1%.*} -gt ${2%.*} ] then printf "%s is larger than %s\n" "$1" "$2" elif [ ${1%.*} -eq ${2%.*} ] then : decimal comparison left as exercise for the reader else printf "%s is smaller than %s\n" "$1" "$2" fi
I like this approach.
Thanks again!
  #7 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 750
OK, and what about:

Code:
echo "12.13>12.14"|/usr/bin/xpg6/bc; echo "12.14>12.13"|/usr/bin/xpg6/bc

Closed Thread

Bookmarks

Tags
comparison, float

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0