本文翻译自:How do I pause my shell script for a second before continuing?
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. 但是,我只想暂停一下while true
以便我的while true
不会崩溃。
I tried pause(1)
, but it says -bash: syntax error near unexpected token '1'
. 我尝试了pause(1)
,但它显示-bash: syntax error near unexpected token '1'
。 How can it be done? 怎么做到呢?
#1楼
参考:https://stackoom.com/question/1SiSE/在继续之前-如何暂停我的Shell脚本一秒钟
#2楼
In Python (question was originally tagged Python) you need to import the time module 在Python中(问题最初被标记为Python),您需要导入时间模块
import time
time.sleep(1)
or 要么
from time import sleep
sleep(1)
For shell script is is just 对于shell脚本来说只是
sleep 1
Which executes the sleep
command. 执行sleep
命令。 eg. 例如。 /bin/sleep
#3楼
Use the sleep
command. 使用sleep
命令。
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; 在指定时间单位时,也可以使用小数。 eg sleep 1.5s
例如sleep 1.5s
#4楼
I realize that I'm a bit late with this, but you can also call sleep and pass the disired time in. For example, If I wanted to wait for 3 seconds I can do: 我意识到我对此有点迟了,但是您也可以致电sleep并传递所需的时间。例如,如果我想等待3秒钟,则可以执行以下操作:
/bin/sleep 3
4 seconds would look like this: 4秒将如下所示:
/bin/sleep 4
#5楼
然后呢:
read -p "Press enter to continue"
#6楼
Within the script you can add the following in between the actions you would like the pause. 在脚本内,您可以在要暂停的操作之间添加以下内容。 This will pause the routine for 5 seconds. 这将使例程暂停5秒钟。
read -p "Pause Time 5 seconds" -t 5
read -p "Continuing in 5 Seconds...." -t 5
echo "Continuing ...."