![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 02: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 10:29 AM |
| arrays in awk??? | craigsky | Shell Programming and Scripting | 3 | 08-27-2007 10:13 PM |
| KSH and arrays | whited05 | Shell Programming and Scripting | 1 | 06-24-2005 01:07 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|