我有这个bash脚本,它基本上启动了带有进度指示器的web和selenium服务器.由于启动selenium服务器需要一些时间,因此我在无限循环中检查状态.
问题是,在等待它开始的时候我按下键意外地显示在屏幕上,如果循环结束(超时),它也会在命令提示符中显示.
我想在循环内部禁用所有用户输入(当然除了控制键):
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
}
Run Code Online (Sandbox Code Playgroud)
用于stty关闭键盘输入.
stty -echo
#### Ur Code here ####
stty echo
Run Code Online (Sandbox Code Playgroud)
-echo关闭键盘输入并stty echo重新启用键盘输入.
stty调用来自http://www.unix.com/shell-programming-and-scripting/84624-nonblocking-io-bash-scripts.html
这仍然尊重Ctrl-C,但不显示输入,并消耗它,因此它不会留给shell.
#!/bin/bash
hideinput()
{
if [ -t 0 ]; then
stty -echo -icanon time 0 min 0
fi
}
cleanup()
{
if [ -t 0 ]; then
stty sane
fi
}
trap cleanup EXIT
trap hideinput CONT
hideinput
n=0
while test $n -lt 10
do
read line
sleep 1
echo -n "."
n=$[n+1]
done
echo
Run Code Online (Sandbox Code Playgroud)