Simulating read -n1 of bash in dash
Simulating read -n 1 of bash in dash is to use stty command in linux.
# using stty -icanon time 0 min 1
echo -n "Type your character: "
stty -echo -icanon time 0 min 1
CHA=$(head -c 1)
stty sane
echo "You typed: "$CHA""
# OR using stty -cbreak
echo -n "Type your character: "
stty -echo -cbreak
CHA=$(head -c 1)
stty sane
echo "You typed: "$CHA""
# using stty -icanon time 0 min 1
# using dd bs=1 count=1
echo -n "Type your character: "
stty -echo -icanon time 0 min 1
CHA=$(dd bs=1 count=1 2>/dev/null)
stty sane
echo "You typed: "$CHA""
# OR using stty -cbreak
# and using dd bs=1 count=1
echo -n "Type your character: "
stty -echo -cbreak
CHA=$(dd bs=1 count=1 2>/dev/null)
stty sane
echo "You typed: "$CHA""
Above approaches work perfectly. See stty man page for more.
It can not be handle with the keys like Arrow Keys or Page Up/Down keys, see: ANSI Escape Code.
Comments
Post a Comment