I have a simple code to ensure a script takes at least x seconds (500 here) on Ubuntu
t1=$(date +%s)
# script is here
t2=$(date +%s)
let "t = 500 - $t2 + $t1"
(( t = t>0 ? t : 1 ))
sleep $t
The code works perfectly, but I believe my coding is not efficient, and these three lines
t2=$(date +%s)
let "t = 500 - $t2 + $t1"
(( t = t>0 ? t : 1 ))
should be expressed in one single line. My question is how to improve the code.
date
?)sleep
, how are you going to unravel that when someone calls you at 4 in the morning the day after a most raucous party to debug it when it goes sideways?t
andt2
entirely, but make sure your phone is switched off at 4am.sleep $( printf "%d\n" $(( 500 - ($(date +%s) - t1) )) | sed 's/^-.*/1/' )