ChatGPT解决这个技术问题 Extra ChatGPT

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.

I tried pause(1), but it says -bash: syntax error near unexpected token '1'. How can it be done?


S
Serge Stroobandt

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


It seems on Mac OS X, the s, m, h, nor d have any impact. You must specify the time in seconds.
I think the naming is because we have thread.sleep function in many programming languages @Geremia
More recently the suffix gives an error on Mac. "usage: sleep seconds"
V
Vonton

And what about:

read -p "Press enter to continue"

Actually, this answered my question, even if it doesn't answer the OP.
this has nothing to do with this post.
@murtadhaalsabbagh, But has very much to do with Google indexing :) Helped me too, btw.
read -p "Press enter to continue" -t 1 should pause for 1 sec
@JesseChisholm I guess people are looking for the bash equivalent of the powershell/cmd PAUSE
J
John La Rooy

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


yes, this says it too, it's just the other one was first, and it has a nice example :) but +1!
So while not the right way to do it, you can combine the python answer with Bash by using python -c "import time; time.sleep(1)" instead of sleep 1 :)
@BerryM. - Takes about 1.2 seconds when I try it. Python doesn't start up instantly - you need to account for that. Make it python -c "import time; time.sleep(0.8)" instead. But then we need to factor in how long python startup actually takes. You need to run this: date +%N; python -c "import time; time.sleep(0)"; date +%N to determine how many nanoseconds python is taking to start. But that also includes some overhead from running date. Run this date +%N; date +%N to find that overhead. Python's overhead on my machine was actually closer to 0.14 seconds. So I want time.sleep(0.86).
True, though you'll never get exactly 1000ms, not even like that.
R
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.


+1 ... cuz if you use one ampersand after the sleep it sends sleep off to its own thread then immediately starts the next action (i.e. the sleep doesn't delay the next action)
Why && and not ;?
@vrnvorona && will wait for the first command to finish.
@RobotBoy i think most importantly, if first command fails, && won't proceed, but ; will. ; will wait too, but won't check success.
p
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:

/bin/sleep 3

4 seconds would look like this:

/bin/sleep 4

A
AnneTheAgile

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

sleep 120

J
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.

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

This wont actually work, will it? I think that -t 5 will abort the script after 5 seconds, not continue, at least according to this man page for read
@BradParks Yes it will work Just in case: root@Joses-iPad:~ # cat readtest.bash #!/bin/bash echo "Here" read -p "Pause Time 5 seconds" -t 5 read -p "Continuing in 5 Seconds...." -t 5 echo "Continuing ...." echo "There" root@Joses-iPad:~ # cat re./st.bash Here Pause Time 5 secondsContinuing in 5 Seconds....Continuing .... There
Hmmm.... I think you're right! I thought I tried this, but I just tried it again and it works as advertised. Thanks!
P
Paul Rooney
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

for more info check read manpage ref 1, ref 2


While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
This answer is a code-only comment. It does not attempt to explain the code given. I feel the poster misinterpreted the question as "How do I wait for user input while simultaneously having a timeout on that user input"
B
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 @

> WAIT_FOR_SECONDS=`/usr/bin/expr $RANDOM % 240` /bin/sleep
> $WAIT_FOR_SECONDS

A
Akhil

use trap to pause and check command line (in color using tput) before running it

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