0

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.

5
  • Basically, is there a more intelligent way of doing this... Commented Jan 24, 2013 at 17:16
  • What about it stackoverflow.com/questions/1624480/… Commented Jan 24, 2013 at 17:22
  • why use read -n 1 and function get_char ? seems to do both the same thing. Commented Jan 24, 2013 at 17:23
  • get_char times out after 1/10th of a second, read n -1 has no timeout, it just waits for a character. Commented Jan 24, 2013 at 17:33
  • I ran your script and its never timed out.. just waiting for key.. i don't know what is the purpose? Commented Jan 24, 2013 at 17:39

1 Answer 1

2

Will replacing your inner while with this do what you want?

read -n 1 fullstring

while true; do
    get_char
    if [ -n "$grabbed_char" ] ; then
        fullstring="$fullstring$grabbed_char"
    else
        break
    fi
done
echo -e "\nFULLSTRING:  !$fullstring!"

Incidentally, on older shells where [...] isn't a built in, replacing the if with this will run faster:

case "$grabbed_char" in
    "")  break ;;
    *) fullstring="$fullstring$grabbed_char" ;;
esac
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.