The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Arrays in awk catwoman Shell Programming and Scripting 5 07-28-2008 01:51 AM
Need Help with awk and arrays fusionX Shell Programming and Scripting 7 02-11-2008 06:41 PM
awk arrays imonthejazz Shell Programming and Scripting 1 09-21-2007 09:29 AM
arrays in awk??? craigsky Shell Programming and Scripting 3 08-27-2007 09:13 PM
KSH and arrays whited05 Shell Programming and Scripting 1 06-24-2005 12:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-06-2008
ricardo.ludwig ricardo.ludwig is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
Export Arrays in ksh

Hello everybody!

Why I can export arrays in ksh?
I trie this for exemplo:

In parent script

array[0]=a
array[1]=b
array[2]=c

export array

When I see the variable array in child script there are only first index.

For exemplo in child script

+echo ${array[*]}
a

But I want all index.
Any idea?
  #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,617
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
  #3 (permalink)  
Old 10-06-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,310

You cannot export arrays.

The easiest way to pass an array is to convert it to a scalar variable, with the elements separated by a character not used in any of the elements. For example, this uses a newline:

Code:
array_separator='
' ## adjust as needed
array=( a b c )
array_contents=$( printf "%s$array_separator" "${array[@]}" )
export array_separator array_contents
To convert it back to an array:

Code:
oIFS=$IFS
IFS=$array_separator
array=( $array_contents )
IFS=$oIFS
  #4 (permalink)  
Old 10-06-2008
ricardo.ludwig ricardo.ludwig is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
Trouble solved

bakunin and cfajohnson thank you so much for your helps.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0