(ksh) problem with scripting

Hello,

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...

Can someone help me?

Thanks and Kind Regards
Mon5tar

I believe I'd rather edit my post ...

I believe you need:

land=${array_land}  

Please post the code of both of your scripts

You are missing an eval statement:

eval land$i=${array_land}

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:
. <( echo "land$i"'=${array_land )

The OP seems to be using a shell that supports arrays, so, in my opinion, no external programs are needed.

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.

What external program are you referring to, eval being a shell builtin ?

His external program ^^^ :smiley:

Could the colon : help in any way ?

I stand corrected, I though eval wasn't implemented as built in all frequently used ksh implementations.

No.

Yes,
given the output the OP posted ...

/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

... I assumed array_zahl contains only integers and most probably eval is unnecessary (i.e. no need to dynamically construct identifiers).

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.

I agree.

Isn't it amazing: how much we learn answering questions?

Yep, definitely :slight_smile: