I am trying to get pretty much the same thing as a:
du -bc | grep total | cut -f1
but using a recursive function in bash (learning exercise).
Trying to figure out what I did wrong.
Here is what I've written:
Code:
#!/bin/bash
#du -bc | grep total | cut -f1
function calcul
{
# echo $1
local sum=0
sum=$(($sum+$(ls -l $1 | cut -d" " -f5)))
cd $1
for f in $( ls -1 ) ; do
if [ -f $f ] ; then
sum=$(($sum+$(ls -l $f | cut -d" " -f5)))
# echo $f
# echo $sum
else
sum=$(($sum+$(ls -l $f | cut -d" " -f5)))
calcul $f
sumrec=$?
sum=$(($sum+$sumrec))
fi
done
cd ..
echo $sum
return $sum
}
calcul .
sumfinal=$?
echo $sumfinal
The output of this script is:
Aka, when returning from the function, it does not display the correct value...
I can't seem to figure out what I did wrong... any suggestions are welcomed.