Reading input in bash in an infinite loop and reacting to it

后端 未结 1 1546
小鲜肉
小鲜肉 2021-01-24 09:29

So I want to wait 1 second for some input (options that i will implement later). Or i want the program to print something(will implement later too). I have run into a problem th

相关标签:
1条回答
  • 2021-01-24 10:34

    I've modified your sample slightly so that it works. There was an error in the read statement. use read varinstead of read $var. This corrected sample will now recognise also the h input.

    Related to your question Why it doesn't wait the second (which was btw. hard to determine so i increased the timeout a bit ;-) )? This is because when you enter something, the read timeout is interrupted. It is as the parameter name say's a timeout for the user input. So if the user input's something the timeout is interrupted.

    #!/bin/bash
    
    while true 
    do
          echo 'wait for input ...'
    
          read -t 10 var
    
          echo 'got input ...'
    
          case $var in
            h)
              echo 'help'
            ;;
          esac
    done
    
    0 讨论(0)
提交回复
热议问题