How to have a bash script loop until a specific time
11,419
Solution 1
You can use date
to print the hours and then compare to the one you are looking for:
while [ $(date "+%H") -lt 20 ]; do
echo "test"
sleep 1
done
as date "+%H"
shows the current hour, it keeps checking if we are already there or in a "smaller" hour.
Solution 2
If you want a specific date, not only full hours, then try comparing the Unix time:
while [ $(date +%s) -lt $(date --date="2016-11-04T20:00:00" +%s) ]; do
echo test
sleep 1
done
Solution 3
Just change true
to the real condition:
while (( $(date +%H) < 20 )) ; do
echo Still not 8pm.
sleep 1
done
Author by
user788171
Updated on June 26, 2022Comments
-
user788171 almost 2 years
Usually to run an infinite bash loop, I do something like the following:
while true; do echo test sleep 1 done
What if instead, I want to do a loop that loops infinitely as long as it is earlier than 20:00. Is there a way to do this in bash?