To create a menu with bash, which reacts after a keypress and not only
after <Return>
is possible, but poorly documented. What used to be choice
under DOS can be written in a bash script like so:
#!/bin/bash
# flush reads characters as long as characters are available
function flush()
{
while [[ true ]]; do
read -t0
case $? in
0) read -n1 ;;
*) break ;;
esac;
done;
}
while [ not $CHOICE ]; do
flush
read -n1 -s -p"[1,2,(3)] " CHOICE
case $CHOICE in
1) echo "selection 1"; break ;;
2) echo "selection 2"; break ;;
'q') break ;;
*) unset CHOICE ;;
esac
echo
done
Zeile 3 Empties the input buffer: All previous keypresses are flushed.
Zeile 4
-n1
makes read
stop after the first char
-s
(=silent) makes the read invisible, chars not echoed to the console
-p
shows the following string as a prompt. We could also archive that with
an echo
instruction before calling read, but we would then need -n
with
echo, so that it adds no newline.