I have written a recursive function to iterate through directories, iteration should exit when the specified depth is reached. Both the entry directory and maximum depth value are passed as parameters to the function.
perform_fragmented_copy() {
echo -e "perform_fragmented_copy: $1 at depth $2"
while [ "$1" ] && [ "$2" -le $n_max_depth ];
do
declare -i n_entry_size=$(du -sb $1 | awk '{print $1}')
if [ $sz_remainig_destination -gt $n_entry_size ]
then
echo -e "perform_fragmented_copy: can do the copy";
else
echo -e "perform_fragmented_copy: cannot, iterating again";
declare -i n_curr_depth=$(($2+1))
perform_fragmented_copy "$1"/* $n_curr_depth
fi
shift
done
echo -e "perform_fragmented_copy: exiting: $1 at depth $2"
}
Invoked as
sz_remainig_destination=10000
n_max_depth=5
perform_fragmented_copy $1 0
Output is:
perform_fragmented_copy: /home/ROSS-82/ at depth 0
perform_fragmented_copy: cannot, iterating again
perform_fragmented_copy: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
./ftest.sh: line 69: [: /home/ROSS-82//ross_reader-82: integer expression expected
perform_fragmented_copy: exiting: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
./ftest.sh: line 69: [: : integer expression expected
perform_fragmented_copy: exiting: 0 at depth
In output at fist recursion $2
is received as integer which is 0
perform_fragmented_copy: /home/ROSS-82/ at depth 0
In output at second recursion $2
is received as $1
perform_fragmented_copy: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
I have tried many ways to resolve this, and googled for solutions, but no results yet.