0

请帮助如何在此脚本中添加变量,有人知道吗?谢谢你们。

#!/bin/bash

ARROW_UP=???    # I do not know
ARROW_DOWN=???  # I do not know
ARROW_LEFT=???  # I do not know
ARROW_RIGHT=??? # I do not know
ARROW_ENTER=??? # I do not know

case "$KEY" in "$ARROW_UP")    echo "press the up arrow key"
               "$ARROW_DOWN")  echo "press the down arrow key";;
               "$ARROW_LEFT")  echo "press the left arrow key"
               "$ARROW_RIGHT") echo "press the right arrow key"
               "$ARROW_ENTER") echo "press the enter key"
esac
4

2 回答 2

2

试试这样的事情(你可能想添加一个案例来打破循环):

#!/bin/bash

# Reset terminal to current state when we exit.
trap "stty $(stty -g)" EXIT

# Disable echo and special characters, set input timeout to 0.2 seconds.
stty -echo -icanon time 2 || exit $?

# String containing all keypresses.
KEYS=""

# Set field separator to BEL (should not occur in keypresses)
IFS=$'\a'

# Input loop.
while [ 1 ]; do

    # Read more input from keyboard when necessary.
    while read -t 0 ; do
        read -s -r -d "" -N 1 -t 0.2 CHAR && KEYS="$KEYS$CHAR" || break
    done

    # If no keys to process, wait 0.05 seconds and retry.
    if [ -z "$KEYS" ]; then
        sleep 0.05
        continue
    fi

    # Check the first (next) keypress in the buffer.
    case "$KEYS" in
      $'\x1B\x5B\x41'*) # Up
        KEYS="${KEYS##???}"
        echo "Up"
        ;;
      $'\x1B\x5B\x42'*) # Down
        KEYS="${KEYS##???}"
        echo "Down"
        ;;
      $'\x1B\x5B\x44'*) # Left
        KEYS="${KEYS##???}"
        echo "Left"
        ;;
      $'\x1B\x5B\x43'*) # Right
        KEYS="${KEYS##???}"
        echo "Right"
        ;;          
    esac
done

更多细节在这里

于 2012-10-24T12:30:56.797 回答
2

简短的回答是你不能。使用真正的编程语言。

可以在这里找到一些过于复杂的解决方案,但我不认可它们。同页底部的KEYBD陷阱解决方案不错,但需要ksh93。

于 2012-10-24T12:33:08.103 回答