Results 1 to 8 of 8
Hi,
I have this command in ksh:
1. typeset -i INTEGER
INTEGER=aaa
In all platforms except of Linux it is failing and return exit code 1
Why in Linux it ...
- 11-18-2010 #1Just Joined!
- Join Date
- Nov 2008
- Posts
- 6
typeset -i in Linux
Hi,
I have this command in ksh:
1. typeset -i INTEGER
INTEGER=aaa
In all platforms except of Linux it is failing and return exit code 1
Why in Linux it is not failing? I must it for my number tests
2. The same for printf command. Only in Linux the bellow command is not failing:
printf "%d" aaa
- 11-18-2010 #2
typeset may automatically determine the base16 representation of the number passed in?
"aaa" (base16) = "101010101010" (base2) = "2730" (base10)
on which other system (non-linux) does this command fail? solaris? mac?
- 11-18-2010 #3Just Joined!
- Join Date
- Nov 2008
- Posts
- 6
On AIX/HP/Sun it is failing:
$ typeset -i INTEGER
$ INTEGER=aaa
ksh: aaa: bad number
- 11-18-2010 #4
it will be implementation (and hence also version) specific. it's not the first time that a command works on one box but not on the other with a slightly older kernel.
man typeset yields for the -i option:
-i[base]
An integer. base represents the arithmetic base from 2 to 64. The option value may be omitted. The default value is 10.
[...]
IMPLEMENTATION
version
typeset (AT&T Research) 2008-08-04
author
David Korn <dgk@research.att.com>
copyright
Copyright © 1982-2010 AT&T Intellectual Property
license
http://www.opensource.org/licenses/cpl1.0.txt
- 11-19-2010 #5Just Joined!
- Join Date
- Jul 2008
- Posts
- 81
Have you tried
$ INTEGER=0xaaa
- 11-19-2010 #6
Do a proper input sanity check.
Something like:
which is in pseudo code something like:Code:[ $INTEGER -ge 0 -o $INTEGER -le 0 ] 2> /dev/null && echo "Yes it is an integer"
Code:if ( $INTEGER >= 0 ) or ( $INTEGER <= 0 ) then print "Yes it is an integer" endif
- 11-19-2010 #7Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
I assume you’re porting ksh scripts to Linux. You will find minor differences in how ksh behaves on Linux than non-Linux systems. I know full well because we had to port ksh scripts from Tru64 to Linux and the minor differences caused us headaches. With your example Linux ksh will assign the value of 0 when you try to assign a non-numeric value to an integer variable.
As mentioned on Linux you’d have to validate the data to mimic the behavior on the non-Linux systems. Here’s a (crude) example:Code:#!/bin/ksh -v typeset -i INTEGER INTEGER=aaa echo $INTEGER 0
Code:VALUE=aaa if [[ $(echo $VALUE | grep -q '^[0-9]*$'; echo $?) -eq 0 ]] then INTEGER=$VALUE else echo “ksh: $VALUE: bad number” exit 1 fi
- 11-19-2010 #8Just Joined!
- Join Date
- Nov 2008
- Posts
- 6
Thanks lomcevak.
This is exactly what i did i Linux, but like that:
Code:echo ${NUMBER_TO_TEST} | egrep -q "^[-+]?([1-9][0-9]*|0)$" if [[ $? -eq 0 ]] then return ${__EXIT_STATUS_SUCCESS} else return ${__EXIT_STATUS_FAILED} fi


Reply With Quote