strange ksh arithmetic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting strange ksh arithmetic
# 8  
Old 02-03-2009
Hi.

This seems to work for me in the 3 shells noted:
Code:
#!/usr/bin/env bash
#!/usr/bin/env zsh
#!/usr/bin/env ksh

# @(#) s1       Demonstrate feature.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) grep
set -o nounset

FILE=${1-data1}
echo
echo " Contents of data file $FILE:"
cat $FILE

# Initialize to help isolate errors.
n=""
nan=""

n=23
nan=`grep -i -c nan $FILE`

echo
echo " Results:"
echo " Before arithmetic:  nan = $nan, n = $n"
(( n = n - nan ))
echo " After  arithmetic:  nan = $nan, n = $n"

n=5
nan=-3
echo
echo " Results:"
echo " Before arithmetic:  nan = $nan, n = $n"
(( n = n - nan ))
echo " After  arithmetic:  nan = $nan, n = $n"

exit 0

Producing:
Code:
% ./s1 data2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
grep (GNU grep) 2.5.1

 Contents of data file data2:
Now is the time
for all good men
to come to the aid
of their country.

 Results:
 Before arithmetic:  nan = 0, n = 23
 After  arithmetic:  nan = 0, n = 23

 Results:
 Before arithmetic:  nan = -3, n = 5
 After  arithmetic:  nan = -3, n = 8

For some shells, (( and let allow:
Quote:
Shell variables are allowed as operands; parameter expansion is per-
formed before the expression is evaluated. Within an expression, shell
variables may also be referenced by name without using the parameter
expansion syntax. The value of a variable is evaluated as an arith-
metic expression when it is referenced. A shell variable need not have
its integer attribute turned on to be used in an expression.

-- excerpt from man bash
For the specific case mentioned by the OP, perhaps printing out the values, using "-x", etc., will help pin-point the problem ... cheers, drl
# 9  
Old 02-03-2009
Hi.

The versions of ksh 93s and 93t failed as noted by the OP. Confirmed success with bash and zsh on Debian lenny and Fedora 10 ... cheers, drl
# 10  
Old 02-04-2009
Quote:
Originally Posted by fbg
Hello,

I would like to understand this... I'm using ksh and doing

(( z = y - 1 ))

It is better to use the standard POSIX syntax for arithmetic:

Code:
z=$(( $y - 1 ))

Quote:

if y=34, then the result for z is 33, but if y=034 the result is z=27. Why??

Numbers beginning with 0 are interpreted as octal numbers.
# 11  
Old 02-04-2009
Hi.

I think the string nan is an undocumented reserved word in ksh 93. In this script, I changed the variable name from nan to nn, and the expressions worked as we think they should. At the end of the script, I set and echoed the variables nin and nan, which printed differently:
Code:
#!/usr/bin/env ksh

# @(#) s4       Demonstrate arithmetic with (( expression )).

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) grep
set -o nounset

FILE=${1-data3}
echo
echo " Contents of data file $FILE:"
cat $FILE

# Initialize to help isolate errors.
n=""
nn=""

n=23
nn=`grep -i -c nn $FILE`

echo
echo " Results:"
echo " Before arithmetic:  nn = $nn, n = $n"
(( n = n - nn ))
echo " After  arithmetic:  nn = $nn, n = $n"

n=5
nn=-3
echo
echo " Results:"
echo " Before arithmetic:  nn = $nn, n = $n"
(( n = n - nn ))
echo " After  arithmetic:  nn = $nn, n = $n"

nin=""
echo
echo " The string nin is:"
echo $(( nin ))

nan=""
echo
echo " The string nan is:"
echo $(( nan ))

exit 0

Producing:
Code:
$ ./s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-1-686, i686
Distribution        : Debian GNU/Linux 5.0
ksh 93s+
GNU grep 2.5.3

 Contents of data file data3:
Now is the time
nn
for all good men
nn
to come to the aid
of their country.

 Results:
 Before arithmetic:  nn = 2, n = 23
 After  arithmetic:  nn = 2, n = 21

 Results:
 Before arithmetic:  nn = -3, n = 5
 After  arithmetic:  nn = -3, n = 8

 The string nin is:
0

 The string nan is:
nan

The variable nan vaguely bothered me because it means NaN, Not a Number, in IEEE standards (some details at NaN - Wikipedia, the free encyclopedia ). I also noticed a reference to the C function isnan in the man page for ksh (ksh 93).

Best wishes ... cheers, drl
# 12  
Old 02-04-2009
Quote:
Originally Posted by drl
Hi.

I think the string nan is an undocumented reserved word in ksh 93.

No, it's not.

In the code you posted, you don't do anything with $nan, so there is nothing to show the error.

Post code which does show the error.

And use POSIX syntax for arithmetic so that you can compare the results in other shells.
# 13  
Old 02-04-2009
Hi.
Quote:
Originally Posted by cfajohnson

No, it's not.

In the code you posted, you don't do anything with $nan, so there is nothing to show the error.

Post code which does show the error.

And use POSIX syntax for arithmetic so that you can compare the results in other shells.
The last section of the code sets 2 variables, nin and nan to the null string. It then uses POSIX arithmetic to evaluate those expressions. The nin prints as "0", whereas the nan prints as "nan".

That was to show that changing the variable name in the (second) OP code from nan to nn corrects the problem ... cheers, drl
# 14  
Old 02-04-2009
Quote:
Originally Posted by drl
The last section of the code sets 2 variables, nin and nan to the null string.

That was all you needed to post.
Quote:
It then uses POSIX arithmetic to evaluate those expressions. The nin prints as "0", whereas the nan prints as "nan".

On my version of ksh93 (as well as pdksh and bash) that code prints 0 for both cases.
Quote:

That was to show that changing the variable name in the (second) OP code from nan to nn corrects the problem ... cheers, drl

I cannot reproduce the problem. What version of ksh93 are you using?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Arithmetic syntax error while comparing decimal numbers

Hello, I am having a problem when i execute following script on RHEL 6.4. Same script works fine on another machine where I have same version of RHEL and KSH. Below is the rpm and RHEL version. ossvm12(0)> rpm -qa | grep ksh ksh-20100621-19.el6.x86_64 ossvm12(0)> cat... (7 Replies)
Discussion started by: Adithya Gokhale
7 Replies

2. Shell Programming and Scripting

ksh vs perl strange interaction.

Hello everybody, I isolated an issue: please try the following script (under AIX) #!/usr/bin/ksh93 echo "Before Perl: \c" read line echo "|$line|" perl -e 'print "Perl invocation";' echo echo "After Perl: \c" read line echo "|$line|" On the first input, type Ctrl-D. It works... (13 Replies)
Discussion started by: jlovi
13 Replies

3. Shell Programming and Scripting

Arithmetic operations in bash,ksh,sh

Guys, The below expression is valid in which shells (sh,ksh,bash,csh)? VAR1=2 VAR2=$(($VAR1 -2)) Thanks (1 Reply)
Discussion started by: rprajendran
1 Replies

4. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

5. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, Iīve already searched the forum but canīt find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

6. UNIX for Advanced & Expert Users

Strange KSH behaviour - any comments?

As you are probably aware, $# indicates the number of parameters passed into a korn shell script. But this appears to hang around for sunsequent runs...???? A simple script:- #!/usr/bin/ksh echo "#parameters $#" echo "\$1 $1" echo "\$2 $2" I run the script with 0 parameters (all fine) #... (7 Replies)
Discussion started by: gsw_aix
7 Replies

7. UNIX for Dummies Questions & Answers

Longer commands and strange behaviour on ksh

Hi, I was trying to customize this archaic HP-UX box. only shell available is ksh and that too seems to be pretty old and doesn't completely conform to what I read on the web about ksh. Anyway here are my issues: - I wanted to have a dynamic title on xterm or dtterm. I put the following lines... (2 Replies)
Discussion started by: anurags
2 Replies

8. Shell Programming and Scripting

Strange parameter passing problems (KSH)

Hi all, I'm having a rather peculiar problem involving parameter passing with declared functions in my shell script. Hope to get some advice here. A brief description of my code is as follows: However, I'm not getting the results I wanted. If I pass in $rdir, I'm going to end up... (4 Replies)
Discussion started by: rockysfr
4 Replies

9. Shell Programming and Scripting

arithmetic in ksh

Helloo.. I am trying one very simple thing I could not find anything on google.. I have 2 integer variable..and I need to do division...in ksh where $catch and $num are integer variable.. I tryed with this: printf "%0.2f" $final=$catch/$num but it does not work.. any help is... (12 Replies)
Discussion started by: amon
12 Replies

10. Shell Programming and Scripting

Strange behavior from 'read' statements. (ksh - but could be same on other shells)

I'm getting rather frustrated with an interactive script I'm writing. The script is divided up, with section for setting variable at the top, then functions (which make up most of the script) then basically a line at the end which calls the first function- the program moves between the... (5 Replies)
Discussion started by: alexop
5 Replies
Login or Register to Ask a Question