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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass an array to a function in shell script.?
# 1  
Old 06-20-2013
How to pass an array to a function in shell script.?

hi,

I have a array say

SAP_ARRAY[0]="s1.txt"
SAP_ARRAY[1]="s2.txt"

how can i pass this full array to a function.

here is the sample code i am using..

Code:
CHECK_NO_FILES()
{
        FARRAY=$1
        echo "FARRAY[0] = $FARRAY[0]"
        echo "FARRAY[1] = $FARRAY[1]"
       ............
......................... 
}

Code:
SAP_ARRAY[0]="s1.txt"
SAP_ARRAY[1]="s2.txt"

CHECK_NO_FILES $SAP_ARRAY

the above code sends only the 1st element of the array SAP_ARRAY to the function.

i am using bash shell.
# 2  
Old 06-20-2013
Try this:
Code:
CHECK_NO_FILES "${SAP_ARRAY[*]}"

# 3  
Old 06-20-2013
ok the above code works.. now the next thing.
i am taking each element of this array n checking if that file exist in a directory or not..
Code:
CHECK_NO_FILES()
{
        FARRAY=$1
        echo "FARRAY[0] = $FARRAY[0]"
        echo "FARRAY[1] = $FARRAY[1]"
        i=0
        for file_name in "${FARRAY[@]}"; do
                
                echo "file_name = {$file_name}"
                NO_FILES[$i]=`ls -1 /app/etc/$file_name | wc -l`
                echo "NO_FILES[$i] = ${NO_FILES[$i]}"
                i=`expr $i + 1`

        done
}

S_FILES="s1.txt s2.txt"      # a simple variable.
SAP_ARRAY=($S_FILES)     # this line assign each space delimited element to the array.
echo "SAP_ARRAY[0] = $SAP_ARRAY[0]"   # prints SAP_ARRAY[0] = s1.txt
echo "SAP_ARRAY[1] = $SAP_ARRAY[1]"   # prints SAP_ARRAY[1] = s2.txt

CHECK_NO_FILES "${SAP_ARRAY[*]}"

in the above code sample, my requirement was to take each element of the array and check whether that file exists in a directory or not. and $NO_FILES array will contain the number of files of type i.e.

Code:
NO_FILES[0]=1 ,  if 1 "s1.txt" file is there in the specified directory.
NO_FILES[1]=0 ,  if no "s2.txt" files are found

but in the above function

Code:
echo "file_name = {$file_name}"

the above line returns
Code:
file_name = {s1.txt s2.txt}

expected is
Code:
file_name = {s1.txt}

# 4  
Old 06-20-2013
When you parse the array to the function, you'll have to create the array again from $@ within the function:
Code:
FARRAY=($@)

# 5  
Old 06-20-2013
it works fine.. but actually i am passing 2 parameter to the function CHECK_NO_FILES

1. the array
2. the path location where the files needs to be searched..
check out the last line in the below code..

Code:
CHECK_NO_FILES()
{
        FARRAY=$1
        LOCATION=$2

        FARRAY=($@)  # FARRAY contains s1.txt,s2.txt and the LOCATION value too.

        i=0
        for file_name in "${FARRAY[@]}"; do
                
                echo "file_name = {$file_name}"
                NO_FILES[$i]=`ls -1 $LOCATION/$file_name | wc -l`
                echo "NO_FILES[$i] = ${NO_FILES[$i]}"
                i=`expr $i + 1`

        done
}

S_FILES="s1.txt s2.txt"      # a simple variable.
SAP_ARRAY=($S_FILES)     # this line assign each space delimited element to the array.
echo "SAP_ARRAY[0] = $SAP_ARRAY[0]"   # prints SAP_ARRAY[0] = s1.txt
echo "SAP_ARRAY[1] = $SAP_ARRAY[1]"   # prints SAP_ARRAY[1] = s2.txt

CHECK_NO_FILES "${SAP_ARRAY[*]}" "/app/etc"


now when i use
Code:
FARRAY=($@)

and parse the FARRAY, it contains
Code:
FARRAY[0]=s1.txt
FARRAY[1]=s2.txt
FARRAY[2]=/app/etc

i dnt want the last "/app/etc" as an element in the array "FARRAY".
# 6  
Old 06-20-2013
then set FARRAY=($1) and it should work.

or you can have the location as the first parameter and shift it
This User Gave Thanks to Subbeh For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

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

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 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 parameter to User defined function in shell script?

Hello, Can anyone guide me tin passing parameters into user defined function of shell script (KSH). Here is my code, InsertRecord() { DB_TBL=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF set head off set feed off set serveroutput on INSERT INTO TBL1 ( OLD_VAL, NEW_VAL, ... (7 Replies)
Discussion started by: Poonamol
7 Replies

6. Shell Programming and Scripting

What is the maximum number of parameter we can pass to a shell script function?

what is the maximum number of parameter we can pass to a shell script function (8 Replies)
Discussion started by: alokjyotibal
8 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

How to pass arguments to a function in a shell script?

Hi, I have two shell variables $t1 and $t2 which I need to pass to a function in a shell script. The function will do some computation with those two variables and echo the resultant. But I do not know how to pass teh arguments. The function written is f1() {...... ........ } What should... (3 Replies)
Discussion started by: preetikate
3 Replies
Login or Register to Ask a Question