Export Arrays in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Export Arrays in ksh
# 1  
Old 10-06-2008
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  
Old 10-06-2008
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  
Old 10-06-2008

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  
Old 10-06-2008
Trouble solved

bakunin and cfajohnson thank you so much for your helps.
Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh arrays

Hi, I have a ksh script in which I need to fill an array with a list of filenames. It currently works like this: set -A array \ val1 \ val2 \ val3However, I was wondering why it's not possible to do something like this to make it easier to parse values to the array: set -A array... (3 Replies)
Discussion started by: Subbeh
3 Replies

2. Shell Programming and Scripting

Multidimentional arrays in KSH

Hi, I am using KSH shell and need to define a multi dimensional array in which i need to store and retrive data. Please provide your valuable in puts. Eg: asbjkasd 1234 asdhnas 1254 i need to store the above values and use them as input to another command. Note each line is a pair. Thanks... (8 Replies)
Discussion started by: hraj1984
8 Replies

3. Shell Programming and Scripting

how can I export an alias and use it in any shell that I want to switch to like bash, ksh

None of the aliases that I set are available if I switch to a different shell. How can I export aliases and make them available in any shell that I switch to like ksh or bash ? I tried these $>alias godata='cd /home/kc/app/data' $>alias -x godata='cd /home/kc/app/data' $>alias |... (2 Replies)
Discussion started by: kchinnam
2 Replies

4. Shell Programming and Scripting

export variable from ksh script doesn't work

Hi there, in a script I have #!/usr/bin/ksh TEST=hello export TEST Problem is, that the variable doesn't get exported. I also tried typeset -x TEST=hello When I put the two lines in my .profile, the variable is set fine. Whats could be the problem here? (4 Replies)
Discussion started by: doc_symbiosis
4 Replies

5. UNIX for Dummies Questions & Answers

Arrays in nawk and ksh

I'm not confident at all on how arrays work. I want to know how to set arrays in ksh and in nawk (is there a difference??) if someone can show me some examples of both that will be great. Tried to look up on the net but was confusing me more. Any help would be appreciated. (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

6. UNIX for Dummies Questions & Answers

ksh export

Hi, What is the difference between the following two usage of export? export VARIABLE_NAME;VARIABLE_NAME=value and export VARIABLE_NAME=value Cheers, Vish (1 Reply)
Discussion started by: z_vishnu
1 Replies

7. Shell Programming and Scripting

List of arrays in ksh?

Hi all, In a loop, i am creating an array of integer and adding it to another array that hold arrays // psuedo code pttrnArray=$type$data // This is array of integers. pttrnArrays=${pttrnArray} // This is supposed to be array of arrays. But when i print pttrnArrays, i get only the... (1 Reply)
Discussion started by: jakSun8
1 Replies

8. Shell Programming and Scripting

Automatic Arrays in ksh

Given a line of text in ksh: string1 string2 string3 .....stringn is there a way of automatically assigning each string to an array element? Or just different variables would do. Thanks, Jon (1 Reply)
Discussion started by: Jonny2Vests
1 Replies

9. Shell Programming and Scripting

ksh script - arrays

hello all.. I have been browsing / searching through the forum and have yet not been able to find what I was looking for. I am fairly new to ksh and my task is to create a function that reads in an input file: ***************** 2 3 1 abc def ghi /dev/sid/ *****************... (13 Replies)
Discussion started by: sidamin810
13 Replies

10. Shell Programming and Scripting

KSH and arrays

hello all, I browsed the forum (briefly) and I am having issues with a script I writing. I need to check a directory and see if there are files there, if so process all of them. The issues I am having is that when I create the array of files names using set -A filenames "$(ls -1... (1 Reply)
Discussion started by: whited05
1 Replies
Login or Register to Ask a Question