|
BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?
I'm working on making a menu system on an HP-UX box with Bash on it. The old menu system presents the users with a standard text menu with numbers to make selections. I'm re-working the system and I would like to provide something more akin to iterative search in Emacs.
I have a list of 28 objects in an array that I want to match against as they type. So lets say the first four elements are:
ALU
BOR
CAD
CHR
If the user started with an "a" they would automatically get ALU since there are no other A elements. If they typed "c" however, they would get CAD with CHR printed just beneath it until they either type "a", "h" or something else. If they type "a" as the next letter, they get CAD and CHR disappears from the list. It they type "h" as the next letter, they get CHR and CAD disappears from the list. If they type "x" for example, then they get a brief "No Match" message and return to "C".
My thought is that to pull this off I need something other than 'read' since that requires you to press the enter key. I need something that will capture the keystroke and append it to a variable like this:
Pass 1: (user enters C)
MATCH="C"
# code here to check the entry against the list in the array
echo $MATCH # This prints out "C"
Pass 2: (user enters A)
MATCH=$MATCH$ENTRY
# code here to check the entry against the list in the array
echo $MATCH # This prints out "CA"
Pass 3: (user presses backspace)
MATCH=${MATCH:0:$((${#MATCH}-1))}
# code here to check the entry against the list in the array
echo $MATCH # This prints out "C" again
Pass 4: (user enters X)
MATCH=$MATCH$ENTRY
# code here to check the entry against the list in the array
# no match so...
echo "No Match" ; sleep 2s ; clear
MATCH=${MATCH:0:$((${#MATCH}-1))}
echo $MATCH # This prints out "C"
Is this even a possibility in Bash or should I be looking elsewhere?
|