Linux 相当于 DOS 暂停是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/92802/
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
What is the Linux equivalent to DOS pause?
提问by jt.
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?
我有一个 Bash shell 脚本,我想在其中暂停执行,直到用户按下某个键。在 DOS 中,这可以通过“暂停”命令轻松完成。我可以在我的脚本中使用 Linux 等价物吗?
采纳答案by Jim
read
does this:
read
做这个:
user@host:~$ read -n1 -r -p "Press any key to continue..." key
[...]
user@host:~$
The -n1
specifies that it only waits for a single character. The -r
puts it into raw mode, which is necessary because otherwise, if you press something like backslash, it doesn't register until you hit the next key. The -p
specifies the prompt, which must be quoted if it contains spaces. The key
argument is only necessary if you want to know which key they pressed, in which case you can access it through $key
.
该-n1
指定它只是等待一个字符。在-r
将其放入原始模式,这是必要的,否则,如果按类似反斜杠,它不会注册,直到你遇到下一个关键。在-p
指定的提示,如果包含空格,必须用引号括起来。key
仅当您想知道他们按下了哪个键时才需要该参数,在这种情况下,您可以通过$key
.
If you are using Bash, you can also specify a timeout with -t
, which causes read to return a failure when a key isn't pressed. So for example:
如果您使用 Bash,您还可以使用 指定超时-t
,这会导致 read 在未按下键时返回失败。例如:
read -t5 -n1 -r -p 'Press any key in the next five seconds...' key
if [ "$?" -eq "0" ]; then
echo 'A key was pressed.'
else
echo 'No key was pressed.'
fi
回答by Alex Fort
Try this:
尝试这个:
function pause(){
read -p "$*"
}
回答by xsl
read
without any parameters will only continue if you press enter.
The DOS pause
command will continue if you press any key. Use read –n1
if you want this behaviour.
read
没有任何参数只会在你按下回车键时继续。pause
如果按任意键,DOS命令将继续。read –n1
如果您想要这种行为,请使用。
回答by y.petremann
I use these ways a lot that are very short, and they are like @theunamedguy and @Jim solutions, but with timeout and silent mode in addition.
我使用了很多很短的方法,它们就像@theunamedguy 和@Jim 解决方案,但另外还有超时和静默模式。
I especially love the last case and use it in a lot of scripts that run in a loop until the user presses Enter.
我特别喜欢最后一种情况,并在许多循环运行的脚本中使用它,直到用户按下Enter。
Commands
命令
Enter solution
read -rsp $'Press enter to continue...\n'
Escape solution(with -d $'\e')
read -rsp $'Press escape to continue...\n' -d $'\e'
Any key solution(with -n 1)
read -rsp $'Press any key to continue...\n' -n 1 key # echo $key
Question with preselected choice(with -ei $'Y')
read -rp $'Are you sure (Y/n) : ' -ei $'Y' key; # echo $key
Timeout solution(with -t 5)
read -rsp $'Press any key or wait 5 seconds to continue...\n' -n 1 -t 5;
Sleep enhanced alias
read -rst 0.5; timeout=$? # echo $timeout
输入解决方案
read -rsp $'Press enter to continue...\n'
转义解决方案(使用 -d $'\e')
read -rsp $'Press escape to continue...\n' -d $'\e'
任何关键解决方案(使用 -n 1)
read -rsp $'Press any key to continue...\n' -n 1 key # echo $key
带有预选选项的问题(带 -ei $'Y')
read -rp $'Are you sure (Y/n) : ' -ei $'Y' key; # echo $key
超时解决方案(使用 -t 5)
read -rsp $'Press any key or wait 5 seconds to continue...\n' -n 1 -t 5;
睡眠增强别名
read -rst 0.5; timeout=$? # echo $timeout
Explanation
解释
-rspecifies raw mode, which don't allow combined characters like "\" or "^".
-r指定原始模式,不允许像“\”或“^”这样的组合字符。
-sspecifies silent mode, and because we don't need keyboard output.
-s指定静默模式,因为我们不需要键盘输出。
-p $'prompt'specifies the prompt, which need to be between $' and ' to let spaces and escaped characters. Be careful, you must put between single quotes with dollars symbol to benefit escaped characters, otherwise you can use simple quotes.
-p $' prompt'指定提示,需要在 $' 和 ' 之间让空格和转义字符。请注意,您必须在单引号之间加上美元符号以使转义字符受益,否则您可以使用简单的引号。
-d $'\e'specifies escappe as delimiter charater, so as a final character for current entry, this is possible to put any character but be careful to put a character that the user can type.
-d $' \e'指定 escappe 作为分隔符,因此作为当前条目的最后一个字符,可以放置任何字符,但要小心放置用户可以键入的字符。
-n 1specifies that it only needs a single character.
-n 1指定它只需要一个字符。
-especifies readline mode.
-e指定 readline 模式。
-i $'Y'specifies Y as initial text in readline mode.
-i $“ ÿ”指定Ÿ在readline的模式初始文本。
-t 5specifies a timeout of 5 seconds
-t 5指定超时 5 秒
keyserve in case you need to know the input, in -n1 case, the key that has been pressed.
key服务以防您需要知道输入,在 -n1 情况下,是已按下的键。
$?serve to know the exit code of the last program, for read, 142 in case of timeout, 0 correct input. Put $? in a variable as soon as possible if you need to test it after somes commands, because all commands would rewrite $?
$? 用于知道最后一个程序的退出代码,用于读取,超时时为 142,正确输入为 0。把美元?如果您需要在某些命令之后对其进行测试,请尽快将其放入变量中,因为所有命令都会重写 $?
回答by mikeserv
read -n1
is not portable. A portable way to do the same might be:
read -n1
不便携。一种可移植的方式来做同样的事情可能是:
( trap "stty $(stty -g;stty -icanon)" EXIT
LC_ALL=C dd bs=1 count=1 >/dev/null 2>&1
) </dev/tty
Besides using read
, for just a press ENTER
to continue prompt you could do:
除了使用 read
,只需按ENTER
一下继续提示,您可以执行以下操作:
sed -n q </dev/tty
回答by mwfearnley
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.
如果您只需要暂停循环或脚本,并且您很高兴按 Enter 而不是任何键,那么read
它自己就可以完成这项工作。
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.
它不是最终用户友好的,但在您为自己编写快速脚本的情况下可能就足够了,并且您需要暂停它以在后台手动执行某些操作。
回答by BuvinJ
This worked for me on multiple flavors of Linux, where some of these other solutions did not (including the most popular ones here). I think it's more readable too...
这对我在多种版本的 Linux 上都有效,而其中一些其他解决方案没有(包括这里最受欢迎的解决方案)。我认为它也更具可读性......
echo Press enter to continue; read dummy;
Note that a variable needs to be supplied as an argument to read
.
请注意,需要将变量作为参数提供给read
。
回答by SDsolar
Yes to using read
- and there are a couple of tweaks that make it most useful in both cron
and in the terminal.
是使用read
- 并且有一些调整使其cron
在终端和终端中最有用。
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
.
-n 120 使 read 语句在 2 分钟后超时,因此它不会阻塞 in cron
。
In terminal it gives 2 minutes to see how long the rsync
command took to execute.
在终端中,它有 2 分钟的时间来查看rsync
命令执行所需的时间。
Then the subsequent echo
is so the subsequent bash prompt will appear on the next line.
那么接下来的echo
就是这样后续的bash提示会出现在下一行。
Otherwise it will show on the same line directly after "continue..." when Enteris pressed in terminal.
否则,当Enter在终端中按下时,它将在“继续...”之后直接显示在同一行上。
回答by Tom Hale
This function works in both bash
and zsh
, and ensures I/O to the terminal:
此函数在bash
和中均可用zsh
,并确保终端的 I/O:
# Prompt for a keypress to continue. Customise prompt with $*
function pause {
>/dev/tty printf '%s' "${*:-Press any key to continue... }"
[[ $ZSH_VERSION ]] && read -krs # Use -u0 to read from STDIN
[[ $BASH_VERSION ]] && </dev/tty read -rsn1
printf '\n'
}
export_function pause
Put it in your .{ba,z}shrc
for Great Justice!
把它放在你.{ba,z}shrc
的大正义中!