Bash: How to end infinite loop with any key pressed?

前端 未结 6 759
长发绾君心
长发绾君心 2020-11-30 03:16

I need to write an infinite loop that stops when any key is pressed.

Unfortunately this one loops only when a key is pressed.

Ideas please?

#         


        
6条回答
  •  猫巷女王i
    2020-11-30 04:02

    You need to put the standard input in non-blocking mode. Here is an example that works:

    #!/bin/bash
    
    if [ -t 0 ]; then
      SAVED_STTY="`stty --save`"
      stty -echo -icanon -icrnl time 0 min 0
    fi
    
    count=0
    keypress=''
    while [ "x$keypress" = "x" ]; do
      let count+=1
      echo -ne $count'\r'
      keypress="`cat -v`"
    done
    
    if [ -t 0 ]; then stty "$SAVED_STTY"; fi
    
    echo "You pressed '$keypress' after $count loop iterations"
    echo "Thanks for using this script."
    exit 0
    

    Edit 2014/12/09: Add the -icrnl flag to stty to properly catch the Return key, use cat -v instead of read in order to catch Space.

    It is possible that cat reads more than one character if it is fed data fast enough; if not the desired behaviour, replace cat -v with dd bs=1 count=1 status=none | cat -v.

    Edit 2019/09/05: Use stty --save to restore the TTY settings.

提交回复
热议问题