On Wed, 19 Jun 2013 22:47:35 +0200, David Korn <[email protected]>
wrote:
cc: [email protected]
Subject: Re: [ast-users] ksh93: IFS question
--------
hi,
first of all apologies for this long mail...
I have two questions where I hope for some clarification from the list
(did not find the answers in manpage or the "canonical" book):
I use field splitting with non-default values for IFS in one of my
scripts
within a new-type function
definition supporting local variables. I define locally within the
function body something like
typeset IFS=,
and do my field splitting. after this is completed I do
unset IFS
which I presumed would make the global definition of IFS visible again
for
the rest of the function. but this is not the case
(neither for this special variable nor for any other globally defined
variable): the locally reported value is the null string.
so my first question is
1.
why does the value of a global variable become only visible again after
leaving the function even if the local
variable of the same name is explicitly `unset'? I would have thought
the
global
value is only masked by the local variable as long as the letter is
actually in existence (not yet unset).
more important to me is the the following IFS specific point, however,
which occurs independent of
whether IFS is used locally within the function or globally outside of
any
function:
after I do
unset IFS
field splitting still occurs using the default value of IFS (which
however, is no longer visible
to the user after the unset, e.g.
print "$IFS" | od -bc
shows nothing). when doing the `unset' within the function (with the
locally defined IFS) I assumed that the
`unset' would make the global definition visible again and that,
therefore, field splitting _would_
happen normally but a) the global IFS does in fact not become visible
again before leaving the function (see above)
and b) the same happens at the top level (outside of any function).
if instead I do
IFS=
no further field splitting occurs (as expected). if this description is
unclear, here's an example:
8<----------------------------------------------
s=$'a,b c\td'
print ">>$s<<"
set $s
print "default IFS"
print -n "$IFS"|od -bc
cnt=1
for i in "$@"; do print '$'$((cnt++)): ">>$i<<"; done
IFS=
[[ "$IFS" ]] || echo null
set $s
print "IFS set to null string"
print -n "$IFS"|od -bc
cnt=1
for i in "$@"; do print '$'$((cnt++)): ">>$i<<"; done
unset IFS
[[ "$IFS" ]] || echo null
set $s
print "IFS unset"
print -n "$IFS"|od -bc
cnt=1
for i in "$@"; do print '$'$((cnt++)): ">>$i<<"; done
8<----------------------------------------------
obviously `unset IFS' restores the default value of IFS. however,
the default value is no longer "visible" to the user (test for string
length yields zero etc.).
ultimately, my second question is:
2.
is there a way for the user to know whether `unset IFS' or `IFS=' was
issued "upstream" (apart from looking at the result of an attempted
field
split...)?
and if `unset IFS' silently restores the default value, why is this not
reflected by `print' or the result of an appropriate `test'?
thanks in advance
joerg
thank you for responding.
Variables created inside functions remain until the function completes.
Calling unset does not eliminate the variable, just makes it unset.
I see. so there's no way to delete a variable once it is created?
The standard requires that if IFS is unset, it uses the default $' \t\n'
OK. maybe I overlooked it but is this mentioned in the manpage (or the
book, for that matter)?
it's important to know (I at least didn't), I'd say. I found out about
this only by accident.
Setting IFS= eliminates field splitting characters so no field splitting
is
done.
yes, this at least is obvious from the documentation (= I found it). is
there
any way to discriminate between an unset variable and one to which the null
string is assigned?
joerg
David Korn
[email protected]
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users