如何让bash脚本循环到特定时间

use*_*171 3 bash time loops

通常运行无限bash循环,我执行以下操作:

while true; do
    echo test
    sleep 1
done
Run Code Online (Sandbox Code Playgroud)

如果相反,我想做一个无限循环的循环,只要它早于20:00.有没有办法在bash中这样做?

fed*_*qui 6

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
Run Code Online (Sandbox Code Playgroud)

as date "+%H" shows the current hour, it keeps checking if we are already there or in a "smaller" hour.


mxm*_*nkn 5

如果您想要特定的日期,而不仅仅是完整的时间,请尝试比较 Unix 时间:

while [ $(date +%s) -lt $(date --date="2016-11-04T20:00:00" +%s) ]; do
    echo test
    sleep 1
done
Run Code Online (Sandbox Code Playgroud)