ksh Arithmetic syntax error while comparing decimal numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh Arithmetic syntax error while comparing decimal numbers
# 1  
Old 08-13-2015
HP 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.

Code:
ossvm12(0)> rpm -qa | grep ksh
ksh-20100621-19.el6.x86_64
[/root]
ossvm12(0)> cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.4 (Santiago)

With same RHEL and rpm version the script works fine on another machine.
I am trying to figure out what can effect this simple check "-ge" in ksh.

Code:
#!/bin/ksh
i=10
j=10.1

if [ $i -ge $j ]
then
echo "$i ge $j"
fi

In above script i have hard coded i and j for demo purpose. I have given -x option to get the debug info. Below is how i get it it.

Code:
[root@snmp1 ~]# /tmp/t.ksh
+ i=10
+ j=10.01
+ [ 10 -ge 10.01 ]
/tmp/t.ksh[5]: [: 10.01: arithmetic syntax error
[root@snmp1 ~]

I know "[" will invoke "test" command. But when i have same version fo KSH and RHEL on 2 machines, why does above script have different results ?

Somehow the "-ge" option is not working.

I even verified checksum of /bin/ksh93 which gets shipped in rpm on these 2 machines. Both are same.

What could be the cause ? Any more information needed ? Please help.

Last edited by Adithya Gokhale; 08-13-2015 at 02:32 PM.. Reason: code tags, pls
# 2  
Old 08-13-2015
there's no 'floating number' arithmetic in ksh AFAIK.
# 3  
Old 08-13-2015
Try:-
Code:
if [ $(($i)) -ge $(($j)) ]
.....

---------- Post updated at 06:24 PM ---------- Previous update was at 06:21 PM ----------

Quote:
Originally Posted by vgersh99
there's no 'floating number' arithmetic in ksh AFAIK.
'ksh' can do floating point arithmetic at least on 1993 versions...
# 4  
Old 08-13-2015
Quote:
Arithmetic

KSH-93 allows you to do integer arithmetic in any base from two to sixty-four. You can also do double precision floating point arithmetic. Almost the complete set of C language operators are available with the same syntax and precedence. Arithmetic expressions can be used to as an argument expansion or as a separate command. In addition there is an arithmetic for command that works like the for statement in C.
-- KSH-93 - The KornShell Command and Programming Language
# 5  
Old 08-14-2015
Yes KSH supports floating point arithmetic. I am not getting why its behaving weirdly in 1 machine when it works on the other. What can cause this sort of problem is what I am looking for.

Thanks,
Adithya
# 6  
Old 08-14-2015
The most likely cause is that you're using a 1993 version of ksh on the system where it works, but using a 1988 version of ksh or some non-ksh shell on the other system.

What is the output from the commands:
Code:
type ksh
echo $SHELL
ksh --version
/bin/ksh --version

on the system where your code works.

What is the output from those same commands on the system where it doesn't work?
# 7  
Old 08-14-2015
Solution

Hello,

Thanks all for your time. Actually the problem was with the LC_ALL configuration. On the machine where there was arithmetic syntax error it was "fr_FR.UTF-8".

If I set the LC_ALL to "fr_FR.UTF-8" as shown below I can reproduce the problem.

Code:
root(0)> LC_ALL=fr_FR.UTF-8
[/root]
root(0)> locale
LANG=fr_FR.UTF-8
LC_CTYPE="fr_FR.UTF-8"
LC_NUMERIC="fr_FR.UTF-8"
LC_TIME="fr_FR.UTF-8"
LC_COLLATE="fr_FR.UTF-8"
LC_MONETARY="fr_FR.UTF-8"
LC_MESSAGES="fr_FR.UTF-8"
LC_PAPER="fr_FR.UTF-8"
LC_NAME="fr_FR.UTF-8"
LC_ADDRESS="fr_FR.UTF-8"
LC_TELEPHONE="fr_FR.UTF-8"
LC_MEASUREMENT="fr_FR.UTF-8"
LC_IDENTIFICATION="fr_FR.UTF-8"
LC_ALL=fr_FR.UTF-8
[/root]
root(0)> cat /tmp/t.ksh
#!/bin/ksh

i=10
j=10.01

if [ $i -ge $j ]
then
        echo "$i is ge $j"
else
        echo "$j is ge $i"
fi
[/root]
root(0)> /tmp/t.ksh
/tmp/t.ksh[6]: [: 10.01: arithmetic syntax error
10.01 is ge 10

To solve the problem, set LC_ALL=C.

Code:
root(0)> LC_ALL=C
[/root]
root(0)> /tmp/t.ksh
10.01 is ge 10
[/root]

Thanks,
Adithya

Last edited by Adithya Gokhale; 08-14-2015 at 06:56 AM..
These 2 Users Gave Thanks to Adithya Gokhale For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic syntax error

Hello, I am new to shell scripting and getting the error "arithmetic syntax error" on line 55 of my script. Below is line 55 and 56 Backup=$ (( $Year*365+$Day )) if ] then PrevHostname=$Hostname; PrevBackup=$Backup Error: ./mksysbChk.ksh: line 56: *365+as : arithmetic syntax... (10 Replies)
Discussion started by: hasn318
10 Replies

2. Shell Programming and Scripting

Shell arithmetic : operations on decimal points

i am having a varialbe a , which is input to my file i want to multiply this input with value .43, and assign it to variable b. i tried it as below: #!/bin/sh a=$1 b=`expr $1\*0.43` echo b=$b error : expr: non-integer argument Please tell me , how to do this. Thanks (10 Replies)
Discussion started by: rishifrnds
10 Replies

3. Shell Programming and Scripting

Arithmetic but keep 2 decimal places

I am trying to perform arithmetric, for example, to increment the value of variable $a (say 3) by 0.05 but when I tried the following expression let a=a+0.05 or a=$((a+0.05)) both returned 3.0499999999999998 I want to keep 2 decimal places so it returns 3.05 instead. (6 Replies)
Discussion started by: piynik
6 Replies

4. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

5. Shell Programming and Scripting

Comparing decimal numbers between 0 and 1

For numbers between 0 and 1 the below logic is not working. Output of above shall be "correct" but its echoing "incorrect".Kindly suggest a=.1 if then echo correct else echo incorrect fi Video tutorial on how to use code tags in The UNIX and Linux Forums. (3 Replies)
Discussion started by: itsvikas
3 Replies

6. UNIX for Dummies Questions & Answers

syntax error: invalid arithmetic operator

hi, i have a bash script that i want to receive a a string from another bash file. But because the string has a dot in the middle it gives me an error. The error is in this line: let valor=$1 and the value passed is rules.txt the error is: let: valor=rules.txt: syntax error: invalid... (2 Replies)
Discussion started by: limadario
2 Replies

7. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

8. 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

9. Shell Programming and Scripting

Comparing Decimal Numbers

Im trying to compare two numbers with decimals but its not working as expected. a=1 b=1.1 if then echo "equal" fi When I do this it says that the numbers are equal. Ultimately Im using -le and -ge in the if statements but I tested with -eq for simplicity. Any way to make this... (3 Replies)
Discussion started by: Grizzly
3 Replies

10. Shell Programming and Scripting

comparing two decimal values in KSH

Hi Gurus, I wrote a small KSH script, in that i am comparing two variables like this curr_time = date +%h.%m set_time=23.55 If ]; then zip the file fi The above script is croned to run evey 5(3,8,.......,58) minutes, but it is zipping at 23.03 hours. My intention is at 23.58 .what... (4 Replies)
Discussion started by: nandinisagar
4 Replies
Login or Register to Ask a Question