How to pass array as an arguement ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to pass array as an arguement ?
# 1  
Old 01-15-2009
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 )

Code:
[orcl10gdb@SVRDELLD41 flexiSchema]$ cat test1.sh
#! /usr/bin/ksh
set -a arr1
echo "...In Test1...."
arr1[0]="APPS_DEV"
arr1[1]="TEST_DEV"
echo ${arr1[0]}
echo ${arr1[1]}
sh test2.sh "${arr1[*]}"
[orcl10gdb@SVRDELLD41 flexiSchema]$ cat test2.sh
#! /usr/bin/ksh
set -a arr1 "$@"
echo "...In Test2...."
echo  ${arr1[0]}
[orcl10gdb@SVRDELLD41 flexiSchema]$ sh test1.sh
...In Test1....
APPS_DEV
TEST_DEV
...In Test2....

[orcl10gdb@SVRDELLD41 flexiSchema]$

But it is not working.
Any reason ?

Raj
# 2  
Old 01-15-2009
No idea - works flawless with pdksh and AIX ksh.
You can leave out the extra "sh" in front of calling the second script. You want ksh anyway. Maybe try with another version of ksh.
# 3  
Old 01-15-2009
Even after removing the 'sh' its not working . rather gives the error "test2.sh: command not found"

But it works well if we are passing single Array element as parameter like ${arr1[0]}.( with extra sh Smilie )

Raj.
# 4  
Old 01-15-2009
I did not say it will work when you remove sh - I said it is not needed.
Just use ./ to execute the test2.sh

Also when this are explicit kornshell scripts by using #!/usr/bin/ksh, maybe just call them test1.ksh and test2.ksh, just for matter of "correctness".

Here is my output with pdksh:

Code:
root@isau02:/data/tmp/testfeld> cat test1.ksh
#! /usr/bin/ksh
set -a arr1
echo "...In Test1...."
arr1[0]="APPS_DEV"
arr1[1]="TEST_DEV"
echo ${arr1[0]}
echo ${arr1[1]}
./test2.ksh "${arr1[*]}"
root@isau02:/data/tmp/testfeld> cat test2.ksh
#! /usr/bin/ksh
set -a arr1 "$@"
echo "...In Test2...."
echo  ${arr1[0]}
root@isau02:/data/tmp/testfeld> ksh
# echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2
# exit
root@isau02:/data/tmp/testfeld> ./test1.ksh
...In Test1....
APPS_DEV
TEST_DEV
...In Test2....
APPS_DEV

# 5  
Old 01-15-2009
For the case you want all elements of the array from test1 as a single element of the array in test2, you could write it like this:

Code:
root@isau02:/data/tmp/testfeld> cat test1.ksh
#! /usr/bin/ksh
set -A arr1 "APPS_DEV" "TEST_DEV"
echo "...In Test1...."
echo ${arr1[*]}
./test2.ksh "${arr1[*]}"
root@isau02:/data/tmp/testfeld> cat test2.ksh
#! /usr/bin/ksh

set -A arr2 "$@"
echo "...In Test2...."
echo  ${arr2[0]}
root@isau02:/data/tmp/testfeld> ./test1.ksh
...In Test1....
APPS_DEV TEST_DEV
...In Test2....
APPS_DEV TEST_DEV

# 6  
Old 01-15-2009
Thanx Zaxxon for the suggestion .

But still It gives the same problem as below

Code:
[orcl10gdb@SVRDELLD41 flexiSchema]$ cat test1.sh
#! /usr/bin/ksh
set -a arr1
echo "...In Test1...."
arr1[0]="APPS_DEV"
arr1[1]="TEST_DEV"
echo ${arr1[*]}
sh ./test2.sh  "${arr1[*]}"
[orcl10gdb@SVRDELLD41 flexiSchema]$ cat test2.sh
#! /usr/bin/ksh
#set -a arr2 "$@"
echo "...In Test2...."
echo  "array all :${arr2[*]} "
echo  "array 0 :${arr2[0]} "
[orcl10gdb@SVRDELLD41 flexiSchema]$ sh test1.sh
...In Test1....
APPS_DEV TEST_DEV
...In Test2....
array all :
array 0 :
[orcl10gdb@SVRDELLD41 flexiSchema]$

Raj.
# 7  
Old 01-15-2009
Now I changed to ksh and tried as follows.

Code:
[orcl10gdb@SVRDELLD41 flexiSchema]$ cat test1.ksh
#! /usr/bin/ksh
set -a arr1
echo "...In Test1...."
arr1[0]="APPS_DEV"
arr1[1]="TEST_DEV"
echo ${arr1[*]}
./test2.ksh  "${arr1[*]}"
[orcl10gdb@SVRDELLD41 flexiSchema]$ cat test2.ksh
#! /usr/bin/ksh
#set -a arr2 "$@"
echo "...In Test2...."
echo  "array all :${arr2[*]} "
echo  "array 0 :${arr2[0]} "
[orcl10gdb@SVRDELLD41 flexiSchema]$ ksh
$ echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2
$ exit
[orcl10gdb@SVRDELLD41 flexiSchema]$ ./test1.ksh
bash: ./test1.ksh: Permission denied
[orcl10gdb@SVRDELLD41 flexiSchema]$ sh ./test1.ksh
...In Test1....
APPS_DEV TEST_DEV
./test1.ksh: line 7: ./test2.ksh: Permission denied

Now , I changed test.ksh with sh

Code:
[orcl10gdb@SVRDELLD41 flexiSchema]$ cat test1.ksh
#! /usr/bin/ksh
set -a arr1
echo "...In Test1...."
arr1[0]="APPS_DEV"
arr1[1]="TEST_DEV"
echo ${arr1[*]}
sh ./test2.ksh  "${arr1[*]}"
[orcl10gdb@SVRDELLD41 flexiSchema]$  sh ./test1.ksh
...In Test1....
APPS_DEV TEST_DEV
...In Test2....
array all :
array 0 :
[orcl10gdb@SVRDELLD41 flexiSchema]$

Even now . its the same.

Btw, the persmission error happened without sh ?

I tried with ksh ./test1.ksh and ksh ./test2.ksh ( in test1.ksh ) .
Same result Smilie

Raj
 
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

How to pass filename as arguement to awk command?

Hi, I am facing one issue. The awk command works fine if i hardcode the file name but if is pass it as an arguement it doesn't work. For e.g:Below commands works fine awk -v A="$type" '{F=substr($0,23,8) "_LTD_" A ".txt"; print $0 >> F; close(F) }' RL004.txt But the below command does not... (2 Replies)
Discussion started by: Neelkanth
2 Replies

5. 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

6. Shell Programming and Scripting

Perl Function Array Arguement Passing

Hi All, I have some questions regarding array arguements passing for Perl Function. If @array contains 2 items , arguements passing would be like Code_A. But what if @array needs to add in more items, the rest of the code like $_ will have to be modified as well (highlighted in red), which is... (5 Replies)
Discussion started by: Raynon
5 Replies

7. Shell Programming and Scripting

How to pass an array from SHELL to C function

Hi, I have an output generated from a shell script like; 0x41,0xF2,0x59,0xDD,0x86,0xD3,0xEF,0x61,0xF2 How can I pass this value to the C function, as below; int main(int argc, char *argv) { unsigned char hellopdu={above value}; } Regards Elthox (1 Reply)
Discussion started by: elthox
1 Replies

8. 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

9. UNIX for Dummies Questions & Answers

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,... (9 Replies)
Discussion started by: msb65
9 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