Time condition loop in shell

后端 未结 8 1514
故里飘歌
故里飘歌 2020-12-04 10:34

I have just started learning shell script recently, so I don\'t know much about it.

I am trying to find example of time based while loop but not having any luck.

相关标签:
8条回答
  • 2020-12-04 10:59

    date +%s will give you the seconds since the epoch, so something like

    startTime = `date +%s`
    timeSpan = #some number of seconds
    endTime = timeSpan + startTime
    
    while (( `date +%s` < endTime )) ; do
        #code
    done
    

    You might need some edits, since my bash is rusty

    0 讨论(0)
  • 2020-12-04 11:01

    This is exactly what I was looking for, here is a one line solution based on bsravanin's answer:

    end=$((SECONDS+30)); of=$((end-SECONDS)) ; while [ $SECONDS -lt $end ]; do echo $((end-SECONDS)) seconds left of $of ; sleep 1 ; done;

    0 讨论(0)
提交回复
热议问题