如何将 bash 中的 readkey 包装在变量中

how to wrap readkey in bash in a variable

我使用 lirc 和 raspberrypi 构建了一个乐高遥控器。

一切正常,但现在我遇到了问题。火车不断需要红外信号。我写了这个脚本:

#!/bin/bash

while : 
do
  if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi

  keypress=''
  clear;
  echo "Dies ist das Lego

  1 = vorwärts
  2 = rückwärts
  3 = stop
  "
  read -n 1 text
  if [ "$text" = "1" ];
  then
    while [ "x$keypress" = "x" ]; do
      irsend SEND_ONCE LEGO_Combo_Direct  FORWARD_FLOAT
      keypress="`cat -v`"
    done
  fi

  if [ "$text" = "2" ];
  then
    while [ "x$keypress" = "x" ]; do
      irsend SEND_ONCE LEGO_Combo_Direct  BACKWARD_FLOAT
      keypress="`cat -v`"
    done
  fi

  if [ "$text" = "3" ];
  then
    while [ "x$keypress" = "x" ]; do
      irsend SEND_ONCE LEGO_Combo_Direct  BRAKE_BRAKE
      keypress="`cat -v`"
    done
  fi

done

我的问题是脚本在 read -n 1 text 停止。 如果某人可以帮助我将 keypress 用于 $text 脚本将不会停止。

提前致谢:)

看起来有点过于复杂。 怎么样:

#!/bin/bash
#
if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi
    clear;
    echo "Dies ist das Lego

   1 = vorwärts
   2 = rückwärts
   3 = stop
"
keypress=''
command='BRAKE_BRAKE'
while true;
do
    keypress="`cat -v`"
    case "$keypress" in
        "") ;;
        1) command="FORWARD_FLOAT";;
        2) command="BACKWARD_FLOAT";;
        3) command="BRAKE_BRAKE";;
        x) echo "Terminating..."; break;;
    esac
    irsend SEND_ONCE LEGO_Combo_Direct $command
done

并且您肯定想添加一些清理代码以将终端恢复到 icanon 模式....