ksh "typeset -i" and Empty Parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh "typeset -i" and Empty Parameters
# 1  
Old 05-08-2013
ksh "typeset -i" and Empty Parameters

I'm getting different behaviors for "typeset -i" on different systems. In one case unset parameters are 0, and in another case they're empty. Is one of these behaviors correct, or is the behavior here unspecified?

First system:
Code:
$ typeset -i x
$ print $x
0
$ print ${.sh.version}
Version M 93t+ 2009-05-01
$ uname
AIX
$ oslevel -s
7100-01-05-1228

Second system:
Code:
$ typeset -i x
$ print $x

$ print ${.sh.version}
Version M-12/28/93e
$ uname
AIX
$ oslevel -s
6100-04-11-1140

# 2  
Old 05-08-2013
There are many ksh variations, mostly 88 and 93. Typesetting controls many useful characteristics, and I like the zero for nothing. The general idea is to allow ksh to store it in integer not ascii, which speeds things up and ensures you have no fractional parts. However, my old hpux ksh8, ksh93, bash all return nil not zero.
Code:
$ bash -c 'typeset -i x ; echo ">$x<"'
><
$ dtksh -c 'typeset -i x ; echo ">$x<"'
><
$ ksh -c 'typeset -i x ; echo ">$x<"'  
><
$ strings `whence ksh` | grep -i version        Wed May 8 15:06:57 EDT 2013
@(#)Version 11/16/88
version
$ strings `whence dtksh` | grep -i version
@(#)DesktopVersionString
%d: invalid binary script version
Unrecognized version
Version not defined
@(#)Version 12/28/93
@(#)Version M-12/28/93
is a shell builtin version of
versions
versions
is a shell builtin version of
@(#)Version M-12/28/93d
.sh.version
%d: invalid binary script version
@(#)dtksh:        $XConsortium: version.c /main/5 1996/08/30 15:36:49 drk $
version
C_VERSION
OS_VERSION
VERSION
VERSION
VERSION
XCU_VERSION
$ bash --version
GNU bash, version 4.2.39(1)-release (hppa2.0w-hp-hpux11.11)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
sdmw504a-ttyp7-/dhome1/n/nbkodln
$

David Korn is still alive last I checked, so you can ask him. http://en.wikipedia.org/wiki/David_K...ter_scientist) He answered me on my issue of inability to concatente <(...) into longer single string arguments (it is always a word, and breaks free of adjacent strings).

Last edited by DGPickett; 05-08-2013 at 04:14 PM..
# 3  
Old 05-08-2013
Quote:
Originally Posted by DGPickett
"typeset -i" ... allow ksh to store it in integer not ascii, which speeds things up
That was my intention in this case. I wasn't actually looking to format the variable. I'm just using it as a binary flag. If the flag is not empty then my code was assuming that it had been deliberately set. This worked fine until I deployed on AIX 7.1, as shown in the original post.

At this point I've made the code portable by removing "-i" from the typeset command. I'm still curious, though, if either way is correct, or of the language leaves this unspecified.
# 4  
Old 05-08-2013
Code:
typeset -i x=0

should give some portability.?
# 5  
Old 05-08-2013
Quote:
Originally Posted by MadeInGermany
Code:
typeset -i x=0

should give some portability.?
Yeah, but I'm looking to portably implement the other behavior, where an unset parameter is empty.
# 6  
Old 05-08-2013
Yes, one should avoid reading what one did not write, or read very critically! Smilie

One shell / O/S I wandered onto had ksh set to nounset => reject reading nonexistent variables. None of my scripts worked! I had to turn that off quick in my .profile ! Talk about well know defaults, isn't "${some_name_never_set}" well known to be "" ?

It makes code more maintainable to not rely on defaults unless they are well known. For a boolean, you might use set and unset, and skip the content entirely.

---------- Post updated at 03:51 PM ---------- Previous update was at 03:50 PM ----------

AIX writes their own ksh?
# 7  
Old 05-08-2013
Quote:
Originally Posted by DGPickett
For a boolean, you might use set and unset, and skip the content entirely.
Even if I unset the parameter the "Version M 93t+ 2009-05-01" ksh insists on telling me the value is 0:

Code:
$ typeset -i x
$ print $x
0
$ unset x
$ print $x
0
$ set |grep x=
x=0

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. HP-UX

script running with "ksh" dumping core but not with "sh"

Hi, I have small script written in korn shell. When it is called from different script, its dumping core, but no core dump when we run it standalone. And its not dumping core if we run the script using "/bin/sh" instead of "ksh" Can some body please help me how to resolve this issue. ... (9 Replies)
Discussion started by: simhe02
9 Replies

6. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

7. Shell Programming and Scripting

check input = "empty" and "numeric"

Hi how to check input is "empty" and "numeric" in ksh? e.g: ./myscript.ksh k output show: invalid number input ./myscript.ksh output show: no input ./myscript.ksh 10 output show: input is numeric (6 Replies)
Discussion started by: geoffry
6 Replies
Login or Register to Ask a Question