ksh "typeset -i" and Empty Parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh "typeset -i" and Empty Parameters
# 8  
Old 05-08-2013
Quote:
Originally Posted by DGPickett
... ... ...

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 "" ?

... ... ...
According to the standards, if an equivalent of the command:
Code:
set -u

has been run, and an equivalent of the command
Code:
set +u

has not been run to counteract it, then:
Quote:
When the shell tries to expand an unset parameter other than the '@' and '*' special parameters, it shall write a message to standard error and shall not execute the command containing the expansion, but for the purposes of setting the '?' special parameter and the exit status of the shell the command shall be treated as having been executed and returned an exit status of between 1 and 125 inclusive. A non-interactive shell shall immediately exit. An interactive shell shall not exit.
Otherwise, ${unknown_variable} expands to an empty string.
# 9  
Old 05-08-2013
Maybe typeset is beyond set/unset, like a tattoo? Smilie

No need to typeset, just unset or set x and test x for being set.

Somewhere in the man page as copies way below, ksh promises you can test just set and unset, no typeset, but I cannot see it, so maybe just go for blank or not. Your test cost is mostly variable lookup:
Code:
$ dtksh -xc '
 set x ; echo ColonEqualSet  ${x:=Right};echo x=$x
 unset x ; echo ColonEqualUnset  ${x:=Right};echo x=$x
 set x ; echo ColonMinusSet  ${x:-Right};echo x=$x
 unset x ; echo ColonMinusUnset  ${x:-Right};echo x=$x
 set x ; echo ColonPlusSet  ${x:+Right};echo x=$x
 unset x ; echo ColonPlusUnset  ${x:+Right};echo x=$x
 set x ; echo EqualSet  ${x=Right};echo x=$x
 unset x ; echo EqualUnset  ${x=Right};echo x=$x
 set x ; echo MinusSet  ${x-Right};echo x=$x
 unset x ; echo MinusUnset  ${x-Right};echo x=$x
 set x ; echo PlusSet  ${x+Right};echo x=$x
 unset x ; echo PlusUnset  ${x+Right};echo x=$x
 set x ; echo ColonQuerySet  ${x:?Right};echo x=$x
 unset x ; echo ColonQueryUnset  ${x:?Right};echo x=$x
 set x ; echo QuerySet  ${x?Right};echo x=$x
 unset x ; echo QueryUnset  ${x?Right};echo x=$x
'
+ set x
+ echo ColonEqualSet Right
ColonEqualSet Right
+ echo x=Right
x=Right
+ unset x
+ echo ColonEqualUnset Right
ColonEqualUnset Right
+ echo x=Right
x=Right
+ set x
+ echo ColonMinusSet Right
ColonMinusSet Right
+ echo x=Right
x=Right
+ unset x
+ echo ColonMinusUnset Right
ColonMinusUnset Right
+ echo x=
x=
+ set x
+ echo ColonPlusSet
ColonPlusSet
+ echo x=
x=
+ unset x
+ echo ColonPlusUnset
ColonPlusUnset
+ echo x=
x=
+ set x
+ echo EqualSet Right
EqualSet Right
+ echo x=Right
x=Right
+ unset x
+ echo EqualUnset Right
EqualUnset Right
+ echo x=Right
x=Right
+ set x
+ echo MinusSet Right
MinusSet Right
+ echo x=Right
x=Right
+ unset x
+ echo MinusUnset Right
MinusUnset Right
+ echo x=
x=
+ set x
+ echo PlusSet
PlusSet
+ echo x=
x=
+ unset x
+ echo PlusUnset
PlusUnset
+ echo x=
x=
+ set x
dtksh: line 15: x: Right

${parameter:-word}

If parameter is set and is non-null then substitute its value. Oth-
erwise substitute word.

word is not evaluated unless it is to be used as the substituted
string.

In the following example, pwd is executed only if d is not set or
is NULL:

print ${d:-$(pwd)}

If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.

${parameter:=word}

If parameter is not set or is null, set it to word. The value of
the parameter is then substituted. Positional parameters cannot be
assigned to in this way.

word is not evaluated unless it is to be used as the substituted
string.

In the following example, pwd is executed only if d is not set or
is NULL:

print ${d:-$(pwd)}

If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.

${parameter:?word}

If parameter is set and is non-null, substitute its value. Other-
wise, print word and exit from the shell , if the shell is not
interactive. If word is omitted then a standard message is printed.

word is not evaluated unless it is to be used as the substituted
string.

In the following example, pwd is executed only if d is not set or
is NULL:

print ${d:-$(pwd)}

If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.

${parameter:+word}

If parameter is set and is non-null, substitute word. Otherwise
substitute nothing.

word is not evaluated unless it is to be used as the substituted
string.

In the following example, pwd is executed only if d is not set or
is NULL:

print ${d:-$(pwd)}

If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.

Last edited by DGPickett; 05-08-2013 at 06:21 PM..
# 10  
Old 05-09-2013
Quote:
Originally Posted by DGPickett
No need to typeset, just unset or set x and test x for being set.
Well, I'm in a function here, and I don't want to name clash, so I use typeset. The function is a widely-used utility function. I suppose I could follow some naming convention to help prevent clashes, but using typeset to create a local parameter seems cleaner. Preventing the name clash is my main reason for using typeset. Adding "-i" was kind of an afterthought to help performance, but the unexpected result on Version M 93t+ 2009-05-01 was that "-i" causes empty to show up as 0.
# 11  
Old 05-09-2013
Name clashes will be. Does typeset do anything just assigning a value to the variable does not? All variables are local unless exported, and cannot escape their subshell in in any case. I assume shells keep unexported variables in a higher priority local container, to search before going to the exported flat ascii environment container.
# 12  
Old 05-09-2013
Quote:
Originally Posted by Matt Miller
Well, I'm in a function here, and I don't want to name clash, so I use typeset. The function is a widely-used utility function. I suppose I could follow some naming convention to help prevent clashes, but using typeset to create a local parameter seems cleaner. Preventing the name clash is my main reason for using typeset. Adding "-i" was kind of an afterthought to help performance, but the unexpected result on Version M 93t+ 2009-05-01 was that "-i" causes empty to show up as 0.
So, forget about -i and just use:
Code:
typeset x=""

From this discussion, it should be clear that declaring a variable to have an integer value AND expecting it to be able to hold an empty string does not produce portable behavior.
# 13  
Old 05-09-2013
Quote:
Originally Posted by DGPickett
variables ... cannot escape their subshell
Right, and variables explicitly declared with "typeset" can't escape their function. For example:

Code:
x="outside f"
function f
{
    typeset x="inside f"
}
f
print $x
outside f

---------- Post updated at 06:49 PM ---------- Previous update was at 06:46 PM ----------

Quote:
Originally Posted by Don Cragun
forget about -i
Yeah, that's what I ended up doing.

Quote:
Originally Posted by Don Cragun
declaring a variable to have an integer value AND expecting it to be able to hold an empty string does not produce portable behavior
Yeah, I'll just consider the behavior of empty integers to be unspecified.
# 14  
Old 05-09-2013
If a function gets put on a pipe, all bets are off, anyway. If outside X is getting stomped by inside x, there is (), but I guess typeset is cheaper. It forces a local variable generation without interrogation of the two existing variable stores for variables to use.

Is it really maintainable long term even for you to have inside x and outside x floating around the same script?

For boolean, the -i is worth it only if you get c boolean with it like in 'x &&', '(( x || y ))' and such. I guess have 1 and 0 is familiar. I rarely use a boolean, maybe using flow instead.
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