Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting in Korn Shell (exporting array variables)

Status
Not open for further replies.

mdet

Technical User
Jun 11, 2001
53
DE
Hi there,
I need to export a variable that is an array. I didn't find a way til now and it seems to me that arrays are strictly not exportable. Am I right or too stupid?
The ksh man page doesn't mention it.
Sample script (test.sh):
#!/usr/bin/ksh
echo "\$ARRAY: $ARRAY" # the whole thing
echo "\${ARRAY[0]}: ${ARRAY[0]}" # the first element



Command Line Input:
$set -A ARRAY first second
$export ARRAY
$echo $ARRAY
first second
$./test.sh

$



Furthermore inside the above script ARRAY is empty.
Is there anybody out there who can explain that to me. What is the difference between an array and a normal environment variable (if there is such)?.

Any hints are appreciated.
 
Try this :

Export your array :

OIFS=$IFS
IFS="°" # Or another char not used in ARRAY
export ARRAY_Values="${ARRAY[*]}"
IFS=$OIFS

In your script test.sh, import the array :

OIFS=$IFS
IFS="°"
set -A ARRAY $ARRAY_Values
IFS=$OIFS

Jean Pierre.
 
All work fine in my linux and Sun boxes. Your output in command line seems adnormal. If you are really using Korn Shell. The output of 'echo $ARRAY' should be 'first' only. So, I guess that you may be using different shell in command line.

tikual
 
On my AIX box, the export of an array doesn't work :

> cat test.sh
#!/usr/bin/ksh
echo "\$ARRAY : $ARRAY" # the whole thing
echo "\${ARRAY[0]}: ${ARRAY[0]}" # the first element
echo "\${ARRAY[1]}: ${ARRAY[1]}" # the second element

echo "\nVariable(s) ARRAY :"
set | grep ARRAY

> set -A ARRAY first second
> export ARRAY
> echo ${ARRAY[*]}
first second
> set | grep ARRAY
ARRAY[0]=first
ARRAY[1]=second

> ./test.sh
$ARRAY : first
${ARRAY[0]}: first
${ARRAY[1]}:

Variable(s) ARRAY :
_='\nVariable(s) ARRAY :'
ARRAY[0]=first
>


Jean Pierre.
 
Mercy Jean Pierre!
it's not a straightforward or elegant solution but it works fine.
Tikual, I have to apologize, you're right my command line shell is the Z-shell (thanks for your hint I forgot) and a comparison of ksh and zsh man pages revealed differences in referencing arrays and it's elements. But the script definitely forks as a ksh child.
It's still a mystery to me why exporting an array is different from exporting a scalar.

Thanks again.
 
ok, let's see the output by command 'set'. See what is the setting of ARRAY in there.

tikual
 
Hi Tikual,
now I'm stricly on Korn Shell and modified the Skript (just grep'ing the output of "set"):
#!/usr/bin/ksh
set|grep ARRAY


Command Line Input:
$unset ARRAY # to be sure nothing's there
$set -A ARRAY first second
$export ARRAY
$./test.sh
$ARRAY=first

Well, obviously ARRAY is exported as a scalar and for that reason it only holds "first". Now, let's declare ARRAY explicitly as a scalar variable.
Command Line Input:
$unset ARRAY # to be sure nothing's there
$export ARRAY=first
$export ARRAY
$./test.sh
ARRAY=first
$


Same result.
My conclusion is: You can't export arrays the way you do it with scalars. Furthermore Z-shell and korn-shell environments are incompatible regarding array variables. I did the same test with zsh as the command line shell and found out that ARRAY even isn't exported neither as scalar nor as array (the script yields nothing).

mdet.

 
From 'man ksh'

The shell supports a limited one-dimensional array facility.
:
The export built-in command does not handle arrays properly.
Only the first element of an array is exported to the environment.

 
Lucky you are Ygor,
under Solaris the ksh man page doesn't mention that. Where did you get that from?


mdet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top