Time condition loop in shell

后端 未结 8 1593
故里飘歌
故里飘歌 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条回答
  •  旧时难觅i
    2020-12-04 10:54

    The best way to do this is using the $SECONDS variable, which has a count of the time that the script (or shell) has been running for. The below sample shows how to run a while loop for 3 seconds.

    #! /bin/bash
    end=$((SECONDS+3))
    
    while [ $SECONDS -lt $end ]; do
        # Do what you want.
        :
    done
    

提交回复
热议问题