Disabling user input during an infinite loop in bash

前端 未结 2 555
孤街浪徒
孤街浪徒 2021-02-09 10:22

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 statu

相关标签:
2条回答
  • 2021-02-09 11:01

    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
    
    0 讨论(0)
  • 2021-02-09 11:04

    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.

    0 讨论(0)
提交回复
热议问题