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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array inside an array manas_ranjan UNIX for Advanced & Expert Users 5 06-10-2008 03:25 PM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 09:42 AM
how do I make dynamic parameter names? Or get the value of a parameter evaluated twi Awanka Shell Programming and Scripting 2 04-19-2007 10:37 PM
parameter u263066 Shell Programming and Scripting 5 10-12-2006 04:46 AM
Array as a Parameter heyindy06 Shell Programming and Scripting 3 03-31-2006 04:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-07-2005
newbreed1 newbreed1 is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 10
Array as parameter

Can I pass and array as a function paramter?
Code:
#!/bin/bash

a1[1]=one
a1[2]=two
a2[1]=one
a2[2]=two

function f1()
{
   array1copy=( ${1[@]} )
   array2copy=( ${2[@]} )
   echo "${array1copy[@]}"
   echo "${array2copy[@]}"
}

f1 "${a1[@]}" "${a2[@]}"
I receive: ${1[@]}: bad substitution

Thank you
  #2 (permalink)  
Old 06-08-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Try to pack the arguments into a single variable and then pass that variable to the function. Like this...

Code:
#!/bin/bash

argument1=`echo ${a1[@]}`

argument2=`echo ${a2[@]}`

f1 argument1 argument2
Vino
  #3 (permalink)  
Old 06-08-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
It is possible, but not without any effort:

Code:

function caller {
typeset array[1]="foo"
typeset array[2]="bar"

callee array
return 0
}

function callee {

typeset -i iCnt=0
(( iCnt = 1 ))
while [ $iCnt -le $(eval print \${#$1[*]}) ] ; do
    print - $(eval print \${$1[$iCnt]})
done

return 0
}
bakunin
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 03:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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