bash 如何在继续之前暂停我的 shell 脚本一秒钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21620406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pause my shell script for a second before continuing?
提问by RydallCooper
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
我的计算机崩溃。
I tried pause(1)
, but it says -bash: syntax error near unexpected token '1'
. How can it be done?
我试过了pause(1)
,但它说-bash: syntax error near unexpected token '1'
。怎么做到呢?
回答by RydallCooper
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; e.g. sleep 1.5s
在指定时间单位时也可以使用小数;例如sleep 1.5s
回答by Vonton
And what about:
还有:
read -p "Press enter to continue"
回答by John La Rooy
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. eg. /bin/sleep
哪个执行sleep
命令。例如。/bin/sleep
回答by pcmaster574
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
回答by AnneTheAgile
On Mac OSX, sleep does not take minutes/etc, only seconds. So for two minutes,
在 Mac OSX 上,睡眠不需要几分钟/等,只需几秒钟。所以两分钟,
sleep 120
回答by Robot Boy
Run multiple sleeps and commands
运行多个睡眠和命令
sleep 5 && cd /var/www/html && git pull && sleep 3 && cd ..
This will wait for 5 seconds before executing the first script, then will sleep again for 3 seconds before it changes directory again.
这将在执行第一个脚本之前等待 5 秒,然后在再次更改目录之前将再次休眠 3 秒。
回答by Jose H. Rosa
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 ...."
回答by Wairua
read -r -p "Wait 5 seconds or press any key to continue immediately" -t 5 -n 1 -s
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
按任意一个按钮继续
回答by Balaji Boggaram Ramanarayan
You can make it wait using $RANDOM, a default random number generator. In the below I am using 240 seconds. Hope that helps @
您可以使用默认的随机数生成器 $RANDOM 使其等待。在下面我使用 240 秒。希望有帮助@
> WAIT_FOR_SECONDS=`/usr/bin/expr $RANDOM % 240` /bin/sleep
> $WAIT_FOR_SECONDS
回答by Akhil J
use trap to pause and check command line before running
在运行前使用 trap 暂停并检查命令行
trap 'tput setaf 1;tput bold;echo $BASH_COMMAND;read;tput init' DEBUG
press any key to continue
按任意键继续
use with
set -x
to debug command line
用于
set -x
调试命令行