I have a script that has a loop that will take several days to execute, the loop includes commands that are timed using the $SECONDS variable.
SECONDS=0
for f in *.extension
do
SECONDS=0
some command
if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo -e " Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo -e " Completed in $minutes minute(s) and $seconds second(s)"
else
echo -e " Completed in $SECONDS seconds"
fi
some other command wrapped between another second timer
some other command wrapped between a 3rd timer
done
if (( $SECONDS > 3600 )) ; then
let "hours=SECONDS/3600"
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo -e " Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $SECONDS > 60 )) ; then
let "minutes=(SECONDS%3600)/60"
let "seconds=(SECONDS%3600)%60"
echo -e " Completed in $minutes minute(s) and $seconds second(s)"
else
echo -e " Completed in $SECONDS seconds"
fi
I get no output for the last timer sitting outside the loop. Another way around it, that I have tried and failed at is setting a $beforedate at the top of the script and a $afterdate and calculating the difference to get the time but cant seem to figure it out.
so something like
var1='date'
some command
var2='date'
difference=$(( $(date "$var1" "+%s") - $(date "$var2" "+%s") ))
echo "scale=2 ; $difference/3600" | bc
But that doesnt work either.