Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    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?

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    6
    On AIX/HP/Sun it is failing:

    $ typeset -i INTEGER
    $ INTEGER=aaa
    ksh: aaa: bad number

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    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

  6. #5
    Just Joined!
    Join Date
    Jul 2008
    Posts
    93
    Have you tried
    $ INTEGER=0xaaa

  7. #6
    Just Joined! jippie's Avatar
    Join Date
    May 2006
    Location
    Eindhoven, the Netherlands
    Posts
    76
    Do a proper input sanity check.
    Something like:
    Code:
    [ $INTEGER -ge 0 -o $INTEGER -le 0 ] 2> /dev/null && echo "Yes it is an integer"
    which is in pseudo code something like:

    Code:
    if
        ( $INTEGER >= 0 )
    or
        ( $INTEGER <= 0 )
    then
        print "Yes it is an integer"
    endif

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

    Code:
    #!/bin/ksh -v
    typeset -i INTEGER
    INTEGER=aaa
    echo $INTEGER
    0
    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:
    VALUE=aaa
    if [[ $(echo $VALUE | grep -q '^[0-9]*$'; echo $?) -eq 0 ]]
       then INTEGER=$VALUE
       else echo “ksh: $VALUE: bad number”
             exit 1
       fi

  9. #8
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •