I have an old shell script which needs to be moved to bash. This script prints progress of some activity and waits for user’s commands. If no action is taken by user for 15 seconds screen is redrawn with new progress and timer starts again. Here’s my problem:
I am trying to use read -t 15 myVar – this way after 15 seconds of waiting loop will be restarted. There is however a scenario which brings me a problem:
- screen redrawn and script waits for input (prints ‘
Enter command:‘) - user enters
foobut doesn’t press enter - after 15 seconds screen is again redrawn and script waits for input – note, that
foois not displayed anywhere on the screen (prints ‘Enter command:‘) - user enters
barand presses enter
At this moment variable $myVar holds ‘foobar‘.
What do I need? I am looking for a way to find the first string typed by user, so I could redisplay it after refreshing status. This way user will see:
Enter command: foo
On Solaris I could use stty -pendin to save input into some sort of a buffer, and after refresh run stty pendin to get this input from buffer and print it on a screen.
Is there a Linux equivalent to stty pendin feature? Or maybe you know some bash solution to my problem?
OK, I think I have the solution. I took nhed’s proposition and worked a bit on it 🙂
The main code prints some status and waits for input:
Function waitForUserInput waits 10 seconds for a keypress. If nothing typed – exits, but already entered keys are saved in a buffer. If key is pressed, it is parsed (added to buffer, or removed from buffer in case of backspace). On Enter buffer is saved to another buffer, from which it is read for further processing:
Thanks to all of you for your help!