【问题标题】:how to wrap readkey in bash in a variable如何将bash中的readkey包装在变量中
【发布时间】:2016-04-11 21:12:20
【问题描述】:

我使用 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,则脚本将不再停止。

提前致谢:)

【问题讨论】:

    标签: linux bash keypress


    【解决方案1】:

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

    #!/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 模式......

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 2015-01-31
      • 1970-01-01
      • 2017-05-20
      • 2022-01-22
      • 1970-01-01
      • 2013-08-02
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多