How do I pause my shell script for a second before continuing?

后端 未结 10 1700
借酒劲吻你
借酒劲吻你 2020-11-30 16:26

I have only found how to wait for user input. However, I only want to pause so that my while true doesn\'t crash my computer.

I tried pause(1)

相关标签:
10条回答
  • 2020-11-30 16:30

    You can make it wait using $RANDOM, a default random number generator. In the below I am using 240 seconds. Hope that helps @

    > WAIT_FOR_SECONDS=`/usr/bin/expr $RANDOM % 240` /bin/sleep
    > $WAIT_FOR_SECONDS
    
    0 讨论(0)
  • 2020-11-30 16:36

    On Mac OSX, sleep does not take minutes/etc, only seconds. So for two minutes,

    sleep 120
    
    0 讨论(0)
  • 2020-11-30 16:41

    And what about:

    read -p "Press enter to continue"
    
    0 讨论(0)
  • 2020-11-30 16:42

    In Python (question was originally tagged Python) you need to import the time module

    import time
    time.sleep(1)
    

    or

    from time import sleep
    sleep(1)
    

    For shell script is is just

    sleep 1
    

    Which executes the sleep command. eg. /bin/sleep

    0 讨论(0)
  • 2020-11-30 16:42

    read -r -p "Wait 5 seconds or press any key to continue immediately" -t 5 -n 1 -s

    To continue when you press any one button

    0 讨论(0)
  • 2020-11-30 16:44

    Use the sleep command.

    Example:

    sleep .5 # Waits 0.5 second.
    sleep 5  # Waits 5 seconds.
    sleep 5s # Waits 5 seconds.
    sleep 5m # Waits 5 minutes.
    sleep 5h # Waits 5 hours.
    sleep 5d # Waits 5 days.
    

    One can also employ decimals when specifying a time unit; e.g. sleep 1.5s

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