How To Pass an Array Variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How To Pass an Array Variable
# 1  
Old 09-04-2008
How To Pass an Array Variable

Hi,

I have a master BASH shell script where I define a bunch of variables:

$var1=why
$var2=is
$var3=(this so hard)

I would then like to call another shell script and pass these variables to it:

$script2 $var1 $var2 $var3

This works fine for var1 and var2. However, var3 is an array, and I can't figure out how to pass it to script2.

Is this not possible in bash? I tried exporting it and that didn't work either. How can I pass arrays defined in one script to another script?

Thank you very much for any help.

Mike
# 2  
Old 09-04-2008
Code:
declare -a ary
ary=($3)
echo $1 $2 ${ary[0]} ${ary[1]} ${ary[2]}

Code:
# sh script.sh why is "this so hard"
why is this so hard

# 3  
Old 09-04-2008
Hi Ikon,

First I would just like to say how much I appreciate all your help.

- I do not appear to have "declare". I am using bash on a Macintosh if that helps.

- I am new to Unix and am having trouble deciphering what you wrote. Specifically, which part goes in the "master" script, and which goes in the "called" script. I would like to have something like this if it's possible:

master script code:

#Define array:
array=(1 2 3)

#Run the "called" script using $array as a parameter:
called_script $array

exit 0
# 4  
Old 09-04-2008
An example how to pass an array to another script:

script1:

Code:
#!/bin/sh

$var1=why
$var2=is
$var3=(this so hard)

./script2 $var1 $var2 ${var3[@]}

exit 0

script2:

Code:
#!/bin/sh

echo $1 $2 $3 $4 $5

exit 0

Regards
# 5  
Old 09-04-2008
Hi,

Yes thank you so much that finally worked! A followup question is:

Can you assign the all of the elements of the array to just one parameter? In your example you assigned the array elements 1-3 to $3 $4 $5 respectively. However, the number of elements in this array will change, and so it would nice to just be able to assign them all to one parameter.

Mike
# 6  
Old 09-04-2008
script2
Code:
#!/bin/sh

var1=$1
var2=$2
ar1=($3)


echo "var1: $var1"
echo "var2: $var2"
count=0
for var in ${ar1[@]}
do
echo "array: $var"
done
exit 0

EDIT: not working hold on checking.

Last edited by Ikon; 09-04-2008 at 02:32 PM..
# 7  
Old 09-04-2008
Quote:
Originally Posted by msb65
Hi,

Yes thank you so much that finally worked! A followup question is:

Can you assign the all of the elements of the array to just one parameter? In your example you assigned the array elements 1-3 to $3 $4 $5 respectively. However, the number of elements in this array will change, and so it would nice to just be able to assign them all to one parameter.

Mike
You can fill an array like in script1:

script2:

Code:
#!/bin/sh

array=($3 $4 $5)

echo ${array[0]} ${array[1]} ${array[2]}

exit 0

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass first array value?

Hi, I am creating filesystem for block device, but I want to pass array value one by one acording to block device count. $tmp1 = block device count 3 $blockdevice = So I want to first pass sdb1 alone in loop, how to take only block device seprately from $blockdevice array. (1 Reply)
Discussion started by: stew
1 Replies

2. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

3. UNIX for Dummies Questions & Answers

Pass array to shell and print

How do i pass an array from test4.sh to a function in another shell script test5.sh, basically i am sourcing the test5.sh in test4.sh and printing the contents, but not working below are my trial scripts, please help, thank you. #!/bin/bash # /usr/local/dw/archive/test5.sh print_array() {... (5 Replies)
Discussion started by: Ariean
5 Replies

4. Shell Programming and Scripting

Pass awk array variable to shell

Hi, all suppose I have following myfile (delimited by tab) aa bb cc dd ee ffand I have following awk command: awk 'BEGIN{FS="\t"}{AwkArrayVar_1=$1;AwkArrayVar_2=$2};END{for(i=0; i<NR; i++) print i, AwkArrayVar_1, AwkArrayVar_2,}' myfileMy question is: how can I assign the awk array... (7 Replies)
Discussion started by: littlewenwen
7 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. Shell Programming and Scripting

How to pass an array as arg to a script..

Hi, Please guide to pass an array as a arg to a script... for example, I have a script small.sh to find the small no of given arg as below... #! /bin/sh # this script is for finding the small number set -A arr_no_updates small=$1 i=1 for arr in $@ do if (3 Replies)
Discussion started by: little_wonder
3 Replies

7. UNIX for Dummies Questions & Answers

How to pass array as an arguement ?

Hi experts, I am here again with another Issue. I need to pass the array as parameter / argument to another script. I tried it as follows . ( I got this idea from the link ) $ cat test1.sh #! /usr/bin/ksh set -a arr1 echo "...In Test1...." arr1="APPS_DEV" arr1="TEST_DEV" echo... (16 Replies)
Discussion started by: rajavu
16 Replies

8. Programming

[ C ] multidemensional array pass to functions

Please excuse my ineptitude for a bit as I've been spoiled for the past few months with only writing perl code instead of C. So ok, I've been thinking about some code to change the crc32 values that are held within central directory headers of zip files. Because I'm lazy I decided to just... (3 Replies)
Discussion started by: VRoemer
3 Replies

9. Shell Programming and Scripting

How to pass ksh array to oracle

Hi all.. Does anyone know have an example of passing the contents of a ksharray to oracle? basically I am looking to loop through the contents of a file and store each line into a bash ksh. Once i have this I can then pass the array into an oracle procedure that accepts an array as an... (1 Reply)
Discussion started by: kiranlalka
1 Replies

10. Shell Programming and Scripting

Can we pass array with call by value in function

I want to pass an array in my function, And my function will be changing the elements of the array in the fuction, but it should not affect the values in my array variable of main function (1 Reply)
Discussion started by: ranjithpr
1 Replies
Login or Register to Ask a Question