在bash中无限循环期间禁用用户输入 [英] Disabling user input during an infinite loop in bash

查看:51
本文介绍了在bash中无限循环期间禁用用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个bash脚本,基本上可以启动带有进度指示器的Web和Selenium服务器.由于Selenium服务器启动需要一些时间,因此我正在无限循环中检查状态.

I have this bash script which basically starts the web and selenium servers with progress indicator. Since it takes some time to selenium server to start I'm checking the status in an infinite loop.

问题是,在等待启动时​​,我意外按下了键,它显示在屏幕上,如果循环结束(超时),它也显示在命令提示符下.

The problem is, while waiting for it to start I press keys accidentaly it's displayed in the screen and if the loop ends (times out) it's also displayed in the command prompt too.

我想在循环内禁用所有用户输入(当然不包括控制键):

I want to disable all user input (except the control keys of course) while inside the loop:

start_selenium() {
    echo -n "Starting selenium server"
    java -jar ${selenium_jar} &> $selenium_log &

    # wait for selenium server to spin up! (add -v for verbose output)
    i=0
    while ! nc -z localhost 4444; do
        sleep 1
        echo -n "."
        ((i++))
        if [ $i -gt 20 ]; then
            echo
            echo -e $bg_red$bold"Selenium server connection timed out"$reset
            exit 1
        fi
    done
} 

推荐答案

使用 stty 关闭键盘输入.

stty -echo
#### Ur Code here ####
stty echo

-echo 关闭键盘输入,而 stty echo 重新启用键盘输入.

-echo turns off keyboard input and stty echo reenables keyboard input.

这篇关于在bash中无限循环期间禁用用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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