The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 10-06-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,629
Arrays cannot be exported the same way variables can be. This is a known limitiation in Korn Shell. You can export every single array element (by using the array name this is the first element by default, as you have already experienced), but not the array itself.

Either you do something like:

Code:
(( iCnt = 0 ))
while [ $iCnt -lt ${array[*]} ] ; do
     export array[$iCnt]
     (( iCnt += 1 ))
done
or you do a little trick: you can pass the arrays name as a parameter to a subroutine, yes? Then use eval to copy the array into a local array. Here is an example:

Code:
function pShowArray
{
typeset    chArrayName="$1"
typeset -i iCnt=0

(( iCnt = 0 ))                  # copy array into local (to the function) array
while [ $iCnt -le $(eval print \${#$1[*]}) ] ; do
     typeset aLocalArray[$iCnt]=$(eval print - \${$1[$iCnt]})
     (( iCnt += 1 ))
done

print - "The passed array is named: $chArrayname"
print - "It has ${#aLocalArray[@]} elements, which are:"

(( iCnt = 0 ))
while [ $iCnt -le ${#aLocalArray[@]} ] ; do
     print - "Element #${iCnt} is \"${aLocalArray[$iCnt]}\""
     (( iCnt += 1 ))
done

return 0
}

# ---- function main

typeset aArrOne[0]="foo"
typeset aArrOne[1]="bar"
typeset aArrOne[2]="wee"
typeset aArrOne[3]="duh"

typeset aArrTwo[0]="1"
typeset aArrTwo[1]="2"
typeset aArrTwo[2]="3"

pShowArray aArrayOne

pShowArray aArrayTwo

exit 0
I hope this helps. It is somewhat like passing parameters via a pointer instead of passing them directly, isn't it?

bakunin