Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to display directory content in array

Tags:

arrays

bash

I am trying to a) count how many files I have on my mass filer EMC device, b) load them in an array c) display a count of how many files I have d) bulk load each file in my database e) display the name of the file I just loaded.

This is my code...

export OUT=/path/to/device
P_Array=$(cd ${OUT} ; find . -name "*TXT" | wc -l)
Plen=${#P_Array[@]}
echo "$Plen FILES TO PROCESS."                                       
if [ $Plen -eq 0 ]
then
        echo "`date '+%m/%d/%y %T:'` ZERO FILES."                  
fi

for name in ${P_Array[@]}
do
        ###Database Bulk Load Here###
        echo "`date '+%m/%d/%y %T:'` $name was loaded."
done

Problem A: Plen=${#P_Array[@]} displays a count of 1 when it should be 5 (sandbox env, right now). Problem B: $name display the total number of files instead of the individual file name.

Obviously, this is all wrong. I am sure I have something switched around but I am not sure what it is. Help!

like image 781
Chris Avatar asked Dec 06 '25 02:12

Chris


1 Answers

Since you do wc -l on the result of find it's going to give the number of files. Hence, the P_Array contains only one number. So Plen is just 1.

Change them to:

P_Array=$(cd ${OUT} ; find . -name "*TXT")
Plen=$(cd ${OUT} ; find . -name "*TXT" | wc -l)
like image 112
P.P Avatar answered Dec 08 '25 18:12

P.P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!