I have following problem...
I have two shell-scripts. The first one is only a script to add Arrays (working very good). In the second script I want to read the Arrays (also working good). I don't know how to explain it...
script:
#!/usr/bin/ksh
. test.sh
for i in ${array_zahl
[*]}
do
land$i=${array_land}
done
land6=LUX
echo $land6
output:
/home/gold/Release-Wechsel/Skripte_RW/test3.sh[5]: land1=DE: not found
/home/gold/Release-Wechsel/Skripte_RW/test3.sh[5]: land2=FR: not found
/home/gold/Release-Wechsel/Skripte_RW/test3.sh[5]: land3=NL: not found
/home/gold/Release-Wechsel/Skripte_RW/test3.sh[5]: land4=IT: not found
/home/gold/Release-Wechsel/Skripte_RW/test3.sh[5]: land5=GB: not found
LUX
Why the script want to execute "land1=DE", he only should set the vars land1 to land5...
The eval is to make an extra pass, as you need one pass to construct the variable name and another to use it. eval is a bit like this, but in the same ksh (note the change from double to single quotes to control what gets expanded the first time -- even eval needs that, ksh is not psychic about which $ are first pass):
echo "land$i"'=${array_land}' | ksh
Without eval, you need to:
construct a script using the variables and send it to another ksh to execute, or
write it to a file and source (. file_path) it,
or somthing like this works on Solaris and other UNIX with /dev?/fd/# file descriptors, expanding in a subshell and sourcing a pipe from that subshell:
eval is a built in ksh function to allow an extra pass of evaluation, not an external program. The problem is that the name of a variable has to be constructed from a variable. An associative array could be used. Is that what you meant? Array, barefoot, is just a pile of like sized items you can index by number. Associative arrays are not arrays at all, but string maps. The name is an unfortunate side effect of overloading the array operator [] in the map call.
I also assumed array_zahl contains only integers and I definitely agree using arrays would be more elegant. However, the eval statement would be less intrusive to the rest of the OP code should he already use hardcoded variable names.