Array as argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array as argument
# 1  
Old 09-24-2009
Array as argument

hi

I've 2 bash script, in the first i define an array and I'd like to pass it as argument to the second script.
For example:
first_bash:
Code:
k=(1 2 3 4)
.......
#in the end i call the 2 script

./second_bash $k

then in the second_bash:

Code:
.....
echo "${1[*]}" #this not work

but i'm not able to manage the array in the 2 bash.

Any ideas?

thanks

D.

--------------------------------------------

Hi
I've tried as follow:

Code:
k=(1 2 3 4)
.......
#in the end i call the 2 script

./second_bash "${k[*]}"

and in the second_bash i call it as

Code:
....
passed_arr=$1
echo "${passed_arr[*]}"
....

and it seems like to work. Anyway I'll check in deep. Thanks
D.

Last edited by Dedalus; 09-24-2009 at 10:50 AM..
# 2  
Old 09-28-2009
No i'm wrong, it doesn't work.
In this way I'm passing one array as just one elemnt.
Still looking for a solution

Thanks

D.
# 3  
Old 09-28-2009
In the second script you should do something like:

Code:
passed_arr=( $1 $2 $3 $4 )

# 4  
Old 09-28-2009
Code:
$ls -l
total 12
-rwxr-x---    1 co01387r spweb          52 sep 28 13:19 sh1.sh
-rwxr-x---    1 co01387r spweb          65 sep 28 13:22 sh2.sh

$more sh1.sh 
k=("my" "vector" "other" "colum")

./sh2.sh ${k[*]}

$more sh2.sh 
echo "Total camp:$#"
for camp in $*
do
        echo "Camp:${camp}"
done
$./sh1.sh 
Total camp:4
Camp:my
Camp:vector
Camp:other
Camp:colum

# 5  
Old 09-28-2009
Thank you!
I got it

D.
# 6  
Old 09-28-2009
Quote:
Originally Posted by Franklin52
In the second script you should do something like:

Code:
passed_arr=( $1 $2 $3 $4 )

Code:
passed_arr=( "$@" )

# 7  
Old 09-28-2009
thx for reply Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

2. Shell Programming and Scripting

Creating an array with options as an argument!

Hi Guys, I am writting a c shell script where I have to parse an input argument with options that could be an array. So far I have achieved where I could parse a single argument with option but failed when I try to create an array with the options. For example: This is on terminal window:... (2 Replies)
Discussion started by: dixits
2 Replies

3. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

6. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with -

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

9. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

10. UNIX for Dummies Questions & Answers

array as command line argument !!!!

hello, can any help me how to can pass array as command line argument in korn shell. also how to read a array from command line. thanks spandu (2 Replies)
Discussion started by: spandu
2 Replies
Login or Register to Ask a Question

Featured Tech Videos