What is the Linux equivalent to DOS pause?

2019-01-07 02:01发布

I have a Bash shell script in which I would like to pause execution until the user presses a key. In DOS, this is easily accomplished with the "pause" command. Is there a Linux equivalent I can use in my script?

标签: linux bash shell
9条回答
对你真心纯属浪费
2楼-- · 2019-01-07 02:32

If you just need to pause a loop or script, and you're happy to press Enter instead of any key, then read on its own will do the job.

do_stuff
read
do_more_stuff

It's not end-user friendly, but may be enough in cases where you're writing a quick script for yourself, and you need to pause it to do something manually in the background.

查看更多
时光不老,我们不散
3楼-- · 2019-01-07 02:36

Yes to using read - and there are a couple of tweaks that make it most useful in both cron and in the terminal.

Example:

time rsync (options)
read -n 120 -p "Press 'Enter' to continue..." ; echo " "

The -n 120 makes the read statement time out after 2 minutes so it does not block in cron.

In terminal it gives 2 minutes to see how long the rsync command took to execute.

Then the subsequent echo is so the subsequent bash prompt will appear on the next line.

Otherwise it will show on the same line directly after "continue..." when Enter is pressed in terminal.

查看更多
趁早两清
4楼-- · 2019-01-07 02:36

Try this:

function pause(){
   read -p "$*"
}
查看更多
登录 后发表回答