什么是相当于DOS暂停的Linux呢? [英] What is the Linux equivalent to DOS pause?

查看:104
本文介绍了什么是相当于DOS暂停的Linux呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的Bash shell脚本中,我想,直到用户presses一键暂停执行。在DOS下,这是很容易与暂停命令来完成。是否有一个Linux相当于我可以在我的脚本中使用?

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?

推荐答案

做到这一点:

user@host:~$ read -n1 -r -p "Press any key to continue..." key
[...]
user@host:~$ 

-n1 规定,只等待一个字符。在 -r 将其放入原始模式,这是必要的,因为否则,如果你preSS类似反斜杠,它不会注册,直到你遇到下一个关键。在 -p 指定的提示,如果包含空格,必须用引号括起来。在参数只是,如果你想知道他们pressed哪个键必要的,在这种情况下,你可以通过 $键

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.

如果你正在使用bash,你也可以指定与 -t 超时,这将导致读返回失败时,关键不在于pressed。因此,例如:

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:

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

这篇关于什么是相当于DOS暂停的Linux呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆