8

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
} 

2 Answers 2

13

The stty invocations are from http://www.unix.com/shell-programming-and-scripting/84624-nonblocking-i-o-bash-scripts.html

This still respects Ctrl-C, but doesn't show input, and consumes it so it's not left 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
Sign up to request clarification or add additional context in comments.

4 Comments

So what you've done here is; with stty time 0 min 0 you set the read timeout and min. chars to 0 to avoid read from blocking the IO and simply read the input and it'll go to nowhere. Is that right? I don't understand the use of -icanon and don't you need to trap SIGINT and run the stty sane there?
Right, the input goes to a variable called "line", which nobody uses.
Turning off icanon means the input will be processed immediately, without having to wait for a newline. Try the script without it, and you'll find that read waits, not because of the read timeout, but because there's no newline.
Putting stty sane into a function triggered by EXIT would be a good idea.
8

Use stty to turn off keyboard input.

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

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

5 Comments

This doesn't exactly work. It disables only echo. Whatever is typed is still there, just invisible.
Yes. He wanted to disable display of accidental key presses right?
No, he wanted to disable the input, not the display. If he accidentally types rm -rf ~/* and presses Enter while the script is running, nothing will be displayed but this doesn't help too much.
Maybe instead of thinking about disabling input, he should think about consuming input, so it won't be left for the shell to process.
What I wanted was to disable the input but disabling the display worked for my case. I think that's because I have tail -f /dev/null to leave the script open and only way of exiting is to send interrupt and in that case the accidentally entered keys are cancelled too...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.