I have the function:
function vacatetmp () {
echo "Vacating TMP folder ${1}...."
for i in "$1"/*; do
if [ -f "$i" ]; then
caseit "$i"
elif [ -d "$i" ]; then
vacatetmp "$i"
fi
done
}
It works fine if the content inside the target folder is static – i.e no change to the files while the function is invoked. But the problem is that another function reference in this code as caseit
, can and does add new files to the target folder. Since the target folder list "$1"/*
is an array that is listed when for
is invoked, the new files created by caseit
are NOT added to that array, and thus are not taken care of by the recursion in function vacatetmp
. Can someone please help by suggesting a way to deal with this issue? I would like this function to also take care of new files added by caseit
function.
For clarity sakes, caseit
function looks up a mime type of the $i
file passed to it by vacatetmp
and unzips the files into the target folder "$1"
– since archives can contain multiple directory hierarchies I have no way of knowing how deep the files will be created, which is the reason for using recursive function.
Best Answer
Iterate over the files first, open them, then iterate over the directories.
It would be more thorough to also call
vacatetmp()
from withincaseit()
, at it's end. But I doubt that is needed, and would lead to less maintainable code.