O GNU date
tem uma precisão de nanossegundos via %N
.
every=3 # test every n'th itteration
bsecs=0.95 # break at secs (float)
bnano=$(printf '%0.9f' "$bsecs"); bnano=${bnano/./};
# avoid lead '0' octal clash when time slice < 1 sec
shopt -s extglob; bnano=${bnano#+(0)}
tprev=$(date +%s%N)
for i in {1..24} ;do # just a test loop
if ((i%every==1)) ;then
tnow=$(date +%s%N)
if ((tnow-tprev>=bnano)) ;then
echo "Auto Break! $every itteratons took longer than $bsecs secs"
break
fi
((tprev=tnow))
fi
# do something, eg sleep for testing
sleep 1.$i; echo $i
done