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

CHECK_NO_FILES()
{
        FARRAY=$1
        echo "FARRAY[0] = $FARRAY[0]"
        echo "FARRAY[1] = $FARRAY[1]"
       ............
......................... 
} 
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.

Try this:

CHECK_NO_FILES "${SAP_ARRAY
[*]}"

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

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.

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

echo "file_name = {$file_name}"

the above line returns

file_name = {s1.txt s2.txt}

expected is

file_name = {s1.txt}

When you parse the array to the function, you'll have to create the array again from $@ within the function:

FARRAY=($@)

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

FARRAY=($@)

and parse the FARRAY, it contains

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

then set FARRAY=($1) and it should work.

or you can have the location as the first parameter and shift it

1 Like