I should preface this by saying I have never worked with shell scripts before so this is all new to me. I was able to make something that worked, but is terribly optimized, and I have no idea how to improve it. If anything it's a pretty hilarious script:
#/bin/bash
get_char() {
old_tty_settings=`stty -g`
stty -icanon min 0 time 1
stty cbreak
grabbed_char=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty -cbreak
stty "$old_tty_settings"
}
while true; do
unset char00
unset char01
unset char02
unset char03
unset char04
unset char05
echo -e "\nWaiting for keystroke..."
read -n 1 char00
while true; do
get_char; char01=$grabbed_char
if [ "$char01" != "" ]; then get_char; char02=$grabbed_char
else break
fi
if [ "$char02" != "" ]; then get_char; char03=$grabbed_char
else break
fi
if [ "$char03" != "" ]; then get_char; char04=$grabbed_char
else break
fi
if [ "$char04" != "" ]; then get_char; char05=$grabbed_char
else break
fi
done
fullstring=$char00$char01$char02$char03$char04$char05
echo -e "\nFULLSTRING: !$fullstring!"
done
The list is a lot longer, but I spared you.
Basically, the program needs to sit in terminal forever waiting for a keystroke. After an initial keystroke, it needs to wait a very short amount of time (1/10th of a second in this case) for another keystroke. If no keystroke is registered in that short time, it echos the output (which will be piped with sed commands), and restarts.
It is meant to be used with a barcode scanner, where you may scan 4 characters, or 100 characters. We want to process the data as quickly as possible, meaning we don't want a 1 second delay after something is scanned.
A good solution would be if "charXX" could be created as a character was received. Thanks for any help.
read -n 1and functionget_char? seems to do both the same thing.