Disabling user input during an infinite loop in bash

I have this bash script that basically starts web servers and selenium servers with a progress indicator. Since it takes some time to start a selenium server, I check the status in an infinite loop.

The problem is that, while waiting for it to start, I press keys randomly displayed on the screen, and if the cycle ends (time out), it also appears on the command line.

I want to disable all user inputs (except for 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 } 
+5
source share
2 answers

Use stty to disable keyboard input.

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

-echo disables keyboard input, and stty echo activates keyboard input again.

+6
source

Hidden Calls: http://www.unix.com/shell-programming-and-scripting/84624-nonblocking-io-bash-scripts.html

This still applies to Ctrl-C, but does not display input and consumes it so that it does not remain for the 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 
+5
source

Source: https://habr.com/ru/post/1203774/


All Articles