strange ksh arithmetic


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

Hello,

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

(( z = y - 1 ))

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

Thanks
# 2  
Old 02-03-2009
try
Code:
typeset -i y=0
#............. forces y to be an integer value

# 3  
Old 02-03-2009
The '0' signals the shell to interpret that value as octal. So 034 (decimal 28) minus 1 equals 27 (decimal)
# 4  
Old 02-03-2009
Thanks! I just wanted to understand...
# 5  
Old 02-03-2009
I have another one

I have

n=23

then I do

nan=`grep -c -i 'NaN' file`

so that nan is a variable with the number of lines with the field "NaN" in file. Then I do

(( n = n - nan ))

and when nan=0, instead of n=23 I get n="nan". Why is that?
# 6  
Old 02-03-2009
Because it's (( n = $n - $nan ))
If you want to use a variable, you have to prefix it with '$', as opposed to declaring it. If you want to use a variable in some text, the preferred method would be ${variable}
# 7  
Old 02-03-2009
Thanks pludi, my ksh was updated to ksh-20080202-2.el5 from a 2006 version. Now my old scripts are all crashing with this. The older version was more flexible, I think.
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