I want to do the following in bash: I have a textfile, and want to call a script that reads the file, waits for keystrokes from me and each time a correct button is pressed, it prints exactly one word (the next word) from the textfile.
To elaborate: Let's just say the textfile looks like the following
This is my
text file
and I am looking for keyboard buttons of the group [[:print:]]. So I call my script like
./script.sh textfile, then I press "A" and the Thisappears, then I press the Space button, still only the This, then I type "O" and the is appears, I press maybe "M" a few more times, until all of the text above is printed, and I'm done.
What I already have:
I already know how to wait for a single keypress and then do something if it works:
while true; do
read -rsn1 input
if [[ "$input" =~ "[[:print]]" ]]; then
# do stuff on keypress
fi
done
I also know how to read a file word by word: Read File word by word (I used answer of user Lambert) The combination of the two does not seem to work how I want it though.
If anyone has any idea on how to do this, I'd greatly appreciate it. Also, please remark if I did formulate anything unclear/wrong or if I asked in the wrong place/manner.
Edit for clarification
The Textfile may contain an arbitrary number of lines. Each keypress should print words exactly like it stands in the Textfile, i.e. it conserves spaces and newlines (think of the script I want to achieve as a delayed cat that prints not the whole file at once, but only Word by Word on keystroke)
textfile, or can there be more than one?