Parameter Problem With an Array Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parameter Problem With an Array Variable
# 1  
Old 09-04-2008
Parameter Problem With an Array Variable

Hi,

I have two bash shell scripts:

test:

rrdhcp78-120:test_data msb65$ cat ~/bin/test
#!/bin/sh

array1=()
echo 'elements in array1:' ${#array1[@]}

~/bin/test2 $array1

test2:

rrdhcp78-120:test_data msb65$ cat ~/bin/test2
#!/bin/sh

array2=${1}
echo 'elements in array2:' ${#array2[@]}

When I run test:

rrdhcp78-120:test_data msb65$ ~/bin/test
elements in array1: 0
elements in array2: 1

Why is it that "array1" initially has 0 elements, but when I export the variable to test2, assign it to variable "array2", it then has 1 element?
# 2  
Old 09-04-2008
Cleaning up to make eaiser to read:

test
Code:
array1=()
echo 'elements in array1:' ${#array1[@]}

~/bin/test2 $array1

test2
Code:
#!/bin/sh

array2=${1}
echo 'elements in array2:' ${#array2[@]}

When I run test:

Code:
rrdhcp78-120:test_data msb65$ ~/bin/test
elements in array1: 0
elements in array2: 1

Why is it that "array1" initially has 0 elements, but when I export the variable to test2, assign it to variable "array2", it then has 1 element?
# 3  
Old 09-04-2008
There is nothing in array1...
Code:
array1=()

Passing NULL?? maybe
Code:
~/bin/test2 $array1

# 4  
Old 09-04-2008
If your passing an array you will want this: (once there is something in it)
Code:
~/bin/test2 "$array1"

# 5  
Old 09-04-2008
Hi,

Thanks for your help! However, that only seems to pass the first element of the array.
# 6  
Old 09-04-2008
Code:
~/bin/test2 "${array1[@]}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify the Parameter variable file

Hi I am trying to understand a shell scipt file ( .ksh file ) . In the shell script we are referring a variable . Example : SessLogs=$STAFF_MSTR_DIR/staff_dtls There are no references in the shell script from where the variable "$STAFF_MSTR_DIR" is being read from . Could anyone... (2 Replies)
Discussion started by: Sudheer Maddula
2 Replies

2. Shell Programming and Scripting

Problem with awk array when loading from shell variable

Hi, I have a problem with awk array when iam trying to use awk in solaris box as below..Iam unable to figure out the problem.. Need your help. is there any alternative to make it in arrays from variable values nawk 'BEGIN {SUBSEP=" "; split("101880|110045 101887|110045 101896|110045... (9 Replies)
Discussion started by: cskumar
9 Replies

3. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

4. Shell Programming and Scripting

using an awk internal variable as parameter for an external array

Hello, I am running a bash script under linux which first defines an CA-array like j=0 num1=120.00 num2=10.00 until do CA='echo $num1 + $j*$num2' j=$ done within the later awk section of this same script I want to read data from a file. If the value of the second column is... (3 Replies)
Discussion started by: MotAah
3 Replies

5. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

6. Shell Programming and Scripting

Reading a variable from parameter file

HI all, I have a parameter file with entries like $$Name =Abhinav $$CUTOFF_DATE = 11/11/2209 I am reading a variable from this file using a awk command like : var2=`awk -F"" "/CUTOFF_DATE/{f=$1;}{print f;}" InputFIleName` but facing an error like awk: cmd. line:1:... (3 Replies)
Discussion started by: abhinav192
3 Replies

7. Shell Programming and Scripting

Array as a Parameter

In a ksh shell... I need to pass 12 parameters to a secondary ksh shell. In VB I would just pass an array that contains all of these, but haven't been successful in passing the full array in a KSH shell. Does anyone have an example where you can pass an array as a parameter? here's what i... (3 Replies)
Discussion started by: heyindy06
3 Replies

8. Shell Programming and Scripting

Assign value to a variable in a parameter file

Hi, I have a parameter file and it contains following items $ cat TransactionParams From_Date_Parm=2005-02-25 To_Date_Parm=2005-05-25 Extract_Root_Parm=/detld1/etl/ascential/Ascential/DataStage/Projects/CTI_London/IAM Extract_Type_Parm=Transaction EDW_Database_Parm=hdw_erks... (2 Replies)
Discussion started by: gopskrish
2 Replies

9. Shell Programming and Scripting

How to check parameter variable?

Hello All, I have a script that will email out if the email address is specified as parameter 1. I am using ksh, and then tried the following : email=$1 Following did not work, I am getting error test -z $email test ${email:=" ") -eq " " test -n $email test ${?email} What... (4 Replies)
Discussion started by: negixx
4 Replies

10. UNIX for Dummies Questions & Answers

Array as parameter

Can I pass and array as a function paramter? #!/bin/bash a1=one a1=two a2=one a2=two function f1() { array1copy=( ${1} ) array2copy=( ${2} ) echo "${array1copy}" echo "${array2copy}" } f1 "${a1}" "${a2}" (2 Replies)
Discussion started by: newbreed1
2 Replies
Login or Register to Ask a Question