Korn shell - evaluating string gives wrong results due to limitations | Unix Linux Forums | Solaris

  Go Back    


Solaris The Solaris Operating System, usually known simply as Solaris, is a Unix-based operating system introduced by Sun Microsystems. The Solaris OS is now owned by Oracle.

Korn shell - evaluating string gives wrong results due to limitations

Solaris


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-11-2013
psychocandy psychocandy is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 August 2013, 5:27 AM EDT
Posts: 37
Thanks: 4
Thanked 1 Time in 1 Post
Korn shell - evaluating string gives wrong results due to limitations

Anyone ever seen this? Someone mentioned this the other day....

If you do, for instance, in korn shell, echo $(5.2+2.5), it gives the result of 6 regardless.

Can't remember why but it was the limitation of the korn shell.
Sponsored Links
    #2  
Old 07-11-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 September 2014, 7:53 AM EDT
Location: San Jose, CA, USA
Posts: 4,701
Thanks: 180
Thanked 1,570 Times in 1,337 Posts
Quote:
Originally Posted by psychocandy View Post
Anyone ever seen this? Someone mentioned this the other day....

If you do, for instance, in korn shell, echo $(5.2+2.5), it gives the result of 6 regardless.

Can't remember why but it was the limitation of the korn shell.
The command echo $(5.2+2.5) only prints 6 if you have a utiiity named 5.2+2.5 that prints 6. When I run that command on my system, I get the diagnostic:

Code:
-ksh: 5.2+2.5: not found

If:

Code:
echo $(5.2+2.5)

prints 6 for you, what does the command:

Code:
type 5.2+2.5

print?

With a 1993 or later version of the Korn shell, the command echo $((5.2+2.5)) (note the double sets of parentheses) prints 7.7 . With an earlier version of the Korn shell, the results are probably similar to what you get giving that command to bash; a diagnostic something like:
Code:
syntax error: invalid arithmetic operator (error token is ".2+2.5")

Sponsored Links
    #3  
Old 07-16-2013
psychocandy psychocandy is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 22 August 2013, 5:27 AM EDT
Posts: 37
Thanks: 4
Thanked 1 Time in 1 Post
Not sure of exact command to be honest. Thats the problem.

It was something to do with caused by stack overflow?
    #4  
Old 07-16-2013
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 22 September 2014, 11:16 PM EDT
Location: Paris
Posts: 4,420
Thanks: 16
Thanked 466 Times in 421 Posts
"6" as output is dubious:

Legacy ksh (ksh88, integer arithmetics):
Code:
$ echo $((5.2+2.5))
7

Current ksh (ksh93, floating point arithmetics):
Code:
$ echo $((5.2+2.5))
7.7

Sponsored Links
    #5  
Old 07-16-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 September 2014, 7:53 AM EDT
Location: San Jose, CA, USA
Posts: 4,701
Thanks: 180
Thanked 1,570 Times in 1,337 Posts
Quote:
Originally Posted by jlliagre View Post
"6" as output is dubious:

Legacy ksh (ksh88, integer arithmetics):
Code:
$ echo $((5.2+2.5))
7

Current ksh (ksh93, floating point arithmetics):
Code:
$ echo $((5.2+2.5))
7.7

You may have been mislead by some of my guesses at what might be going on. The original statement was that the command:

Code:
echo $(5.2+2.5)

(note the single set of parentheses) when processed by an unspecified version of the Korn shell produces the output
Code:
6

Note that psychocandy has yet to answer the question: What does the command
Code:
type 5.2+2.5

produce on a system where echo $(5.2+2.5) prints 6 ?
Sponsored Links
    #6  
Old 07-16-2013
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 22 September 2014, 11:16 PM EDT
Location: Paris
Posts: 4,420
Thanks: 16
Thanked 466 Times in 421 Posts
The chance for a command named "5.2+2.5" to exist on the OP machine is low enough to rule it out thus I assume extra parenthesis were simply missing.

In any case, psychocandy doesn't remember the command typed so my guess is he also fails to remember correctly what was displayed by this command.

This leaves not that much to investigate ...

---------- Post updated at 12:24 ---------- Previous update was at 12:21 ----------

Quote:
Originally Posted by psychocandy View Post
It was something to do with caused by stack overflow?
No. Stack overflow is not something to suspect with so little and so low numbers.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
korn shell: check the content of a string of a variable bora99 Shell Programming and Scripting 3 04-24-2012 04:13 AM
String parsing in Korn Shell omoyne Shell Programming and Scripting 3 05-10-2010 12:29 PM
Can't install Unicode::String due to String.so not found PatrickBaer Solaris 5 12-17-2009 09:20 AM
How to: Parse text string into variables using Korn shell shew01 Shell Programming and Scripting 7 05-23-2008 10:01 AM
Script giving wrong results.... mgirinath Shell Programming and Scripting 1 12-21-2005 08:45 PM



All times are GMT -4. The time now is 08:18 AM.