Question

find_date=$(stat -c %y $files | awk '{print $1}')
#Grabbing each file from the array
for file in "${files[@]}"; do
# We get the date part
file_date=''

#Reading the date and breaking by the - mark
IFS="-" read -ra parts <<< "$file"


unset file_date
find_date=$(stat -c %y $files | awk '{print $1}')
echo "File Date: " $find_date



for t in "${find_date[@]}"; do
#Putting the date into the array
if [[ $t == [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] ]]; then
    file_date=$t
    break
fi
done
echo "T: " $t

So everything works but the for loop where it should move to the next file. When i run my script i notice all the errors for the STAT command not working because after it does the first file it is still trying to STAT that file and not the next one in the list

Was it helpful?

Solution 2

This line:

find_date=$(stat -c %y $files | awk '{print $1}')

files is the array, so $files expands to the first element of that array each time through the loop. You want to use the file variable that you use to iterate over the array.

for file in "${files[@]}"; do
    ...
    find_date=$(stat -c %y "$file" | awk '{print $1}')
    #                      ^^^^^^^
    ...
done

OTHER TIPS

reset the file_date so it does not 'remember' the result of the previous loop, ie:

unset file_date
for t in "${find_date[@]}"; do
   #Putting the date into the array
    if [[ $t == [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] ]]; then
        file_date=$t
        break
    fi
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top