Visit Our UNIX and Linux User Community


Pass array to shell and print


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pass array to shell and print
# 1  
Old 03-18-2014
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.

Code:
#!/bin/bash
# /usr/local/dw/archive/test5.sh

print_array()
{
echo $1
i=1

while [ $i -le $# ];
do
  arr[$i]="$2"
  shift
  i=$(( $i + 1 ))
done

printf "%s\n" "$arr[@]"
}

Code:
#!/bin/bash
# test4.sh
. /usr/local/dw/archive/test5.sh
array[0]="Line 1"
array[1]="Line 2"
array[2]="Line 3"

print_array "john cena" "${array[@]}"

Output:
Code:
john cena
[@]

# 2  
Old 03-18-2014
This is wrong: "$arr[@]" It won't assume the [@] is part of the array without { }.

It needs to be: "${arr[@]}"
# 3  
Old 03-18-2014
Is there a way to pass just the name but not need eval or a child ksh to expand it? Perhaps a (local?) alias not a function?
# 4  
Old 03-19-2014
Quote:
Originally Posted by DGPickett
Is there a way to pass just the name but not need eval or a child ksh to expand it? Perhaps a (local?) alias not a function?
Probably not safely. The same kind of doublethink needed to reprocess variable names also processes things like backticks. Read can be used to write to arbitrary variable names, but not read from them, and isn't so useful for arrays either.
# 5  
Old 03-19-2014
If I understand what you're trying to do, try replacing test5.sh with:
Code:
#!/bin/bash
# /usr/local/dw/archive/test5.sh

print_array()
{
	printf "%s\n" "$1"
	shift

	while [ $# -gt 1 ]
	do	printf "%s " "$1"
		shift
	done
	printf "%s\n" "$1"
}

If you then run test4.sh, you get:
Code:
john cena
Line 1 Line 2 Line 3

# 6  
Old 03-19-2014
At least fix this little error:
Code:
printf "%s\n" "${arr[@]}"

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #317
Difficulty: Easy
Ada Lovejoy is often considered the first computer programmer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass and read an array in ksh shell script function.?

I'm able to read & print an array in varaible called "filelist" I need to pass this array variable to a function called verify() and then read and loop through the passed array inside the function. Unfortunately it does not print the entire array from inside the funstion's loop. #/bin/ksh... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

Unable to print python array in shell script loop.

I am unable to loop print a python string array in my unix shell script: ~/readarr.sh '{{ myarr }}' more readarr.sh echo "Parameter 1:"$1 MYARRAY= $1 IFS= MYARRAY=`python <<< "print ' '.join($MYARRAY)"` for a in "$MYARRAY"; do echo "Printing Array: $a" done Can you... (10 Replies)
Discussion started by: mohtashims
10 Replies

3. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

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

5. Shell Programming and Scripting

How to pass an array to a function in shell script.?

hi, I have a array say SAP_ARRAY="s1.txt" SAP_ARRAY="s2.txt" how can i pass this full array to a function. here is the sample code i am using.. CHECK_NO_FILES() { FARRAY=$1 echo "FARRAY = $FARRAY" echo "FARRAY = $FARRAY" ............... (5 Replies)
Discussion started by: Little
5 Replies

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

7. Shell Programming and Scripting

Can we pass an array of strings from a Perl Program to a Shell Script?

Hi Folks, The subject is my question: Can we pass an array of strings from a Perl Program to a Shell Script? Please provide some sample code. Thanks ---------- Post updated at 11:52 PM ---------- Previous update was at 11:43 PM ---------- I got it. Its here:... (0 Replies)
Discussion started by: som.nitk
0 Replies

8. Programming

How to pass C array as input to Shell script

Hi, In the below C code , i want to pass the array to a unix shel script. my script should called as ex myscript 1,2,3 #include <stdio.h> int main() { int a={1,2,3}; } Thanks, Arun (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

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

10. Shell Programming and Scripting

Pass array variabel to awk from shell

Hi I need to pass an array to Awk script from Shell. Can you please tell how to do it? How to pass this array add_ct_arr to an awk script or access it in awk? i=1 while ; do add_ct_arr=$(echo ${adda_count} | awk -v i=$i -F" " '{print $i;}') echo ${add_ct_arr} ... (1 Reply)
Discussion started by: appsguy616
1 Replies

Featured Tech Videos