I'd like to write a shell script which shall be interactive and react on the use of cursor keys. My current approach seems a little complicated. I would determine the escape sequence for the cursor keys using tput kcud1
etc. and then use read -s -n 1 a
to read byte-by-byte, append the byte to a collected string, and then compare the collected string to the determined escape sequences. If one matches, then I can react.
This is problematic because I've got no idea where an escape sequence ends. For instance, tput kcud1
(down arrow) here returns "\eOB"
. I would use a timeout in order to ignore unknown escape sequences (and probably start over if an escape character arrives). This all doesn't look nice to me.
Isn't there a simpler way of reacting on arrow key usage in a shell script?