首页 文章

在继续之前如何暂停我的shell脚本一秒钟?

提问于
浏览
493

我只找到了如何等待用户输入 . 但是,我只想暂停,以便我的 while true 不会崩溃我的电脑 .

我试过了 pause(1) ,但它说 -bash: syntax error near unexpected token '1' . 怎么做到呢?

8 回答

  • 59

    使用 sleep 命令 .

    例:

    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.
    

    在指定时间单位时也可以使用小数;例如 sleep 1.5s

  • 875

    在脚本中,您可以在要暂停的操作之间添加以下内容 . 这将使例程暂停5秒钟 .

    read -p "Pause Time 5 seconds" -t 5
    read -p "Continuing in 5 Seconds...." -t 5
    echo "Continuing ...."
    
  • 23

    在Python(问题最初被标记为Python)中,您需要导入时间模块

    import time
    time.sleep(1)
    

    要么

    from time import sleep
    sleep(1)
    

    对于shell脚本来说就是

    sleep 1
    

    执行 sleep 命令 . 例如 . /bin/sleep

  • 7

    运行多个睡眠和命令

    sleep 5 && cd /var/www/html && git pull && sleep 3 && cd ..
    

    这将在执行第一个脚本之前等待5秒,然后再次休眠3秒再重新更改目录 .

  • 46

    在Mac OSX上,睡眠不需要几分钟/秒,只需几秒钟 . 所以两分钟,

    sleep 120
    
  • 37

    我意识到我有点迟到了,但你也可以打电话给睡眠,然后把时间浪费过去 . 例如,如果我想等待3秒钟,我可以这样做:

    /bin/sleep 3
    

    4秒看起来像这样:

    /bin/sleep 4
    
  • 13

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

    按任意一个按钮继续

  • 2

    那怎么样:

    read -p "Press enter to continue"
    

相关问题