namerefs alternative for KSH88

I have to use KSH88, so going to BASH, perl etc. is not an option.

Below is a much simplified verison of what I am doing (aka ignore my cut command and i not increasing) :slight_smile:

i=1
BIGSTRING="one two three four five six seven eight"

while [[ $i -le 10 ]]; do 
typeset "STRING$i=`echo $BIGSTRING| cut -d' ' -f1-50`"
typeset -n NEWVAR="STRING$i"
do_stuff_here_w_NEWVAR
done

Namerefs are not available in KSH88
So how else i can accomplish the same thing? I need to echo a string through cut and set it to an increasing variable name based on $i then call the new variable.

I have no idea what that broken snippet of code is supposed to be doing. I'm not also sure what you mean by nameref's, since you don't appear to be doing anything that sets any variables except the ones you already told me to ignore...

I'll take a wild guess.

One way to set arbitrary variable names is to use read.

read var$NAME <<EOF
some text
EOF

nameref is an alias for typedef in ksh93.

As far as I can see he is creating an array in a non-standard way.

BIGSTRING='1 2 3 4 5 777 last'
# the simplest way in ksh88
set -A myarray $BIGSTRING
echo ${myarray[0]}  # first element
arrlen=${#myarray[*]}
echo ${myarray[ arrlen -1 ] } # last element in the array.

Quoting from The New Kornshell Command and Programming Language by Bolsky and Korn (copyright 1995):

There is probably a way to get replace the use of name references using eval, but I have other things I need to do tonight and don't have time to prepare and test an example tonight.

Sorry,
Don

1 Like

You could try:

$ STRING1='ABC    XYZ'
$ NEWVAR=STRING1
$ eval 'echo NEWVAR IS ACTUALLY "$'$NEWVAR'"'
NEWVAR IS ACTUALLY ABC    XYZ

as a workaround. But, if I were you, I would change my shell to one that supports namerefs...

1 Like

The standard approach would be to use eval, but that needs to be done carefully, since it could introduce security risks..

What OS and version are you using?

Here is what i ended up with after doing some research on uses of eval.

i=1
BIGSTRING="one two three four five six seven eight"

while [[ $i -le 10 ]]; do 
typeset "STRING$i=`echo $BIGSTRING| cut -d' ' -f1-50`"
# typeset -n NEWVAR="STRING$i"
eval "NEWVAR=\$STRING$i"
do_stuff_here_w_NEWVAR
done

What do you mean security risk?
I am running on RHEL5 (KSH93) & HPUX, SOL5.10 (KSH88)

Using the eval utility to evaluate user supplied strings allows the user to run anything the script has permission to run. There is no problem if all of the strings passed to eval were generated by the application itself and it knows what it is doing. (The eval utility constructs a command by concatenating arguments together, separating each with a <space> character and then having the shell read and execute the constructed command. So you've got tokenization, command parsing, tilde expansion, parameter expansion, command substitution, arithmetic expansion, field splitting, pathname expansion, quote removal, and I/O redirections that can all trip up an unwary script writer.)

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

I'm assuming that the code you have here isn't doing what you want. When I run the commands:

i=1
BIGSTRING="one two three four five six seven eight"

while [[ $i -le 10 ]]; do 
typeset "STRING$i=`echo $BIGSTRING| cut -d' ' -f1-50`"
typeset -n NEWVAR="STRING$i"
    echo $NEWVAR
    i=$((i+1))
done

in a recent ksh, I get the output:

one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight
one two three four five six seven eight

I would have thought you would want something more like:

i=1
BIGSTRING="one two three four five six seven eight"

for NEWVAR in $BIGSTRING
do
        echo "$NEWVAR"
        i=$((i + 1))
done

which produces the following:

one
two
three
four
five
six
seven
eight

which will work in any ksh88 or later Korn shell and in any POSIX conforming shell (including ksh and bash).

Both HPUX and SOL5.10 probably have a ksh93 version installed: /usr/dt/bin/dtksh . If you use that instead of ksh88 then you could use nameref