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
it works fine.. but actually i am passing 2 parameter to the function CHECK_NO_FILES
the array
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"