Unix Shell - dtksh "invalid self reference"?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > dtksh "invalid self reference"?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author dtksh "invalid self reference"?
rdj

2006-10-18, 1:29 pm

I'm trying to run the following script:

--- begin ---
#!/usr/dt/bin/dtksh

BAR="A B C"

for i in $BAR ; do
echo X:$i
typeset -n FOO=BAR
echo Y:$i
unset FOO
echo Z:$i
done
--- end ---

but get the following output:

--- begin ---
X:A
Y:A
Z:A
X:B
/tmp/x[7]: typeset: BAR: invalid self reference
--- end ---

Is this legitimate for possibly a bug?

Thx,
-rdj

Michael Tosch

2006-10-18, 1:29 pm

rdj wrote:
> I'm trying to run the following script:
>
> --- begin ---
> #!/usr/dt/bin/dtksh
>
> BAR="A B C"
>
> for i in $BAR ; do
> echo X:$i
> typeset -n FOO=BAR
> echo Y:$i
> unset FOO
> echo Z:$i
> done
> --- end ---
>
> but get the following output:
>
> --- begin ---
> X:A
> Y:A
> Z:A
> X:B
> /tmp/x[7]: typeset: BAR: invalid self reference
> --- end ---


What does typeset -n do?
I cannot find it in the man pages.

--
Michael Tosch @ hp : com
Jon LaBadie

2006-10-18, 1:29 pm

rdj wrote:
> I'm trying to run the following script:
>
> --- begin ---
> #!/usr/dt/bin/dtksh
>
> BAR="A B C"
>
> for i in $BAR ; do
> echo X:$i
> typeset -n FOO=BAR
> echo Y:$i
> unset FOO
> echo Z:$i
> done
> --- end ---
>
> but get the following output:
>
> --- begin ---
> X:A
> Y:A
> Z:A
> X:B
> /tmp/x[7]: typeset: BAR: invalid self reference
> --- end ---
>
> Is this legitimate for possibly a bug?
>


dtksh is based on a really old version of ksh93 and
does contain many defects corrected in more recent ksh93's.

On Solaris 9's dtksh I get the same error, but not on a
more recent ksh93.

It does seem strange to unset a variable (you actually unset
BAR with the unset FOO line) within a loop that is used to
control the loop. Of course in $BAR should have been replaced
with in a b c, so maybe the defect is dtksh continues to
reference BAR at the top of the loop while ksh93 does not.

rdj

2006-10-18, 7:38 pm


Michael Tosch wrote:
> What does typeset -n do?
> I cannot find it in the man pages.
>
> --
> Michael Tosch @ hp : com


It allows a variable to be used to reference another variable.

rdj

2006-10-18, 7:38 pm


Jon LaBadie wrote:
> dtksh is based on a really old version of ksh93 and
> does contain many defects corrected in more recent ksh93's.
>
> On Solaris 9's dtksh I get the same error, but not on a
> more recent ksh93.
>


I'm running on Solaris 10 (.sh.version='Version M-12/28/93d' ), so I
guess much hasn't changed.

> It does seem strange to unset a variable (you actually unset
> BAR with the unset FOO line) within a loop that is used to
> control the loop. Of course in $BAR should have been replaced
> with in a b c, so maybe the defect is dtksh continues to
> reference BAR at the top of the loop while ksh93 does not.


Hadn't considered that (wasn't aware just how closely bound "typeset
-n" made the two variables), but nice catch. Just FYI, I get the same
error with:

--- begin ---
#!/usr/dt/bin/dtksh

BAR=1

for i in A B C ; do
echo X:$i
typeset -n FOO=BAR
echo Y:$i
unset FOO
echo Z:$i
done
--- end ---

and with

--- begin ---
#!/usr/dt/bin/dtksh

BAR=1

for i in A B C ; do
echo X:$i
typeset -n FOO=BAR
echo Y:$i
#unset FOO
#echo Z:$i
done
--- end ---

So the "unset" may not have the impact suggested. I'll take a look at
installing a pre-packaged more recent version. I don't see it on
sunfreeware, but blastwave has '93n+,REV=2002.12.21', so I'll see what
that does.

-rdj

rdj

2006-10-18, 7:38 pm

Figured out a workaround: replace my "unset VAR" with "typeset +n VAR".

Thx!

bsh

2006-10-18, 7:38 pm

rdj wrote:
> Figured out a workaround: replace my "unset VAR"
> with "typeset +n VAR".


Yes, but there is a fundamental misunderstanding
of the intended function of "typeset -n," and what it
accomplishes. It defines a "namereference", which
always takes as its value another variable name.
Theoretically, this typed variable should never
accept any value that is not in the form of an
accepted variable name. Since neither "1" nor
"A B C" is a proper ANSI variable (or "ksh
compound variable"), it is not surprising that
dtksh/ksh93d flagged an error.

"Typeset -n" accomplishes the same thing as
the usual question on C.U.S. to the effort of how
to dereference not a variable, but that variable's
value as another variable name. To wit:

eval print - "\$${var}" # see why the {...} is important?

and,

eval \$${var}=some-value

It's a FAQ! See:

http://kornshell.com/doc/faq.html # Read Q19 and Q20

=Brian

Jon LaBadie

2006-10-19, 1:25 am

rdj wrote:
> Jon LaBadie wrote:
>
> I'm running on Solaris 10 (.sh.version='Version M-12/28/93d' ), so I
> guess much hasn't changed.
>

....
>
> So the "unset" may not have the impact suggested. I'll take a look at
> installing a pre-packaged more recent version. I don't see it on
> sunfreeware, but blastwave has '93n+,REV=2002.12.21', so I'll see what
> that does.
>


I get the "standalone" ksh93 from kornshell.com (actually it bounces
you to another site for download).
rdj

2006-10-19, 7:23 pm

bsh wrote:
> Theoretically, this typed variable should never
> accept any value that is not in the form of an
> accepted variable name. Since neither "1" nor
> "A B C" is a proper ANSI variable (or "ksh
> compound variable"), it is not surprising that
> dtksh/ksh93d flagged an error.
>


I can see that with

typeset -n FOO=1

but not

BAR=1
typeset -n FOO=BAR

am I missing something?

> "Typeset -n" accomplishes the same thing as
> the usual question on C.U.S. to the effort of how
> to dereference not a variable, but that variable's
> value as another variable name. To wit:
>
> It's a FAQ! See:
>
> http://kornshell.com/doc/faq.html # Read Q19 and Q20
>


Thanks for the link (glad I check past for first match on Q19). At this
point, I'm still not sure I'm misusing or misunderstanding the
functionality as the way I read the FAQ's, there's just two ways of
doing the same thing (dereferencing a variable?).

-rdj

bsh

2006-10-19, 7:23 pm

rdj wrote:
> bsh wrote:
> typeset -n FOO=1; FOO=somevalue


Because the RHS of variable FOO ("1") is not an
acceptable ANSI variable name, the interpreter will
barf when it encounters it, as this is equivalent to
saying:

1=somevalue

.... which is an error.

> BAR=1; typeset -n FOO=BAR


In creating variable FOO, it makes a kind of
"variable symlink" to variable BAR; both will "contain"
the value "1". It is perhaps more illustrative of the
point to rephrase this as:

$ typeset -n FOO=BAR; BAR=1
$ print $FOO
1

Namereferences were created so that variable _names_
could be passed to functions (recall that typeset is
local to the scope of its function) and to support object-
oriented functionality in a future ksh(1) release.

function myfun
{ typeset -n FOO
print $FOO
}

$ var=somevalue
$ myfun var
somevalue

In ksh88(1) the usual technique for indirect variable
substitution is to utilize an "eval":

function myfun
{ eval print - "\${$FOO}"
}

$ var=somevalue
$ myfun var
somevalue


Oops! Those are Q19 and Q20 in Part III of the FAQ:
"SHELL programming QUESTIONS." There are four
sections, each starting from Q1
[vbcol=seagreen]
> At this
> point, I'm still not sure I'm misusing or misunderstanding the
> functionality as the way I read the FAQ's, there's just two ways of
> doing the same thing (dereferencing a variable?).


_If_ you want to perform an _indirect_ dereference -- in
C called an indirection -- the technique used in ksh88 is
allowable, of course, in ksh93, with the additional choice
of doing it with namereferences in the latter version of
kornshell.

=Brian

Jon LaBadie

2006-10-20, 1:22 pm

bsh wrote:
> rdj wrote:
>
> Because the RHS of variable FOO ("1") is not an
> acceptable ANSI variable name, the interpreter will
> barf when it encounters it, as this is equivalent to
> saying:
>
> 1=somevalue
>
> ... which is an error.
>
>
> In creating variable FOO, it makes a kind of
> "variable symlink" to variable BAR; both will "contain"
> the value "1". It is perhaps more illustrative of the
> point to rephrase this as:
>
> $ typeset -n FOO=BAR; BAR=1
> $ print $FOO
> 1
>
> Namereferences were created so that variable _names_
> could be passed to functions (recall that typeset is
> local to the scope of its function) and to support object-
> oriented functionality in a future ksh(1) release.
>
> function myfun
> { typeset -n FOO


probably you meant:

typeset -n FOO=$1

> print $FOO
> }
>
> $ var=somevalue
> $ myfun var
> somevalue
>

bsh

2006-10-22, 7:19 pm


Jon LaBadie wrote:
> bsh wrote:
> typeset -n FOO=$1
> print $FOO
> }


You're correct in that this would be the normal vernacular in
which one whould typically implement the feature of namereference
variables; however, the usage is as intended -- I did not wish to
further complicate the issue, although I did allude to this in the
last paragraph.

Perhaps I should have given the examples outside of functions,
but I was trying to be consistent with the FAQ code....

=Brian

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com