Amila Senadheera
Amila Senadheera

Reputation: 13245

How to loop a .txt file line by line and wait for key board press to continue to read next line?

Following is the current shell command I wrote. When running it prints the first line. Then it read the first letter of the next line and continue avoiding the pause that I expect until a keyboard key press. How to avoid that?

while read p; do
    echo "SEARCHING : $p ..."
    read -p "Press any key to continue... " -n1 -s
    echo -n "0047 SER 127.0.1.1 57000 \"$p\" 3" | nc -u 127.0.1.1 57000
done <Queries

Queries file content

Twilight
Jack
American Idol
Happy Feet
Twilight saga
Happy Feet
Happy Feet
Feet
....
....

output of the shell script

SEARCHING : Twilight ...
SEARCHING : ack ...
SEARCHING : merican Idol ...
SEARCHING : appy Feet ...
SEARCHING : wilight saga ...
....
....

Upvotes: 0

Views: 561

Answers (1)

Andreas Louv
Andreas Louv

Reputation: 47119

The problem is that the body of the while loop inherits the stdin from the while, making the inner read read a line from the Queries-file, a quick fix is to specify input from the tty:

while IFS= read -r p; do
    echo "SEARCHING : $p ..."
    read -p "Press any key to continue... " -n1 -s < /dev/tty
    echo -n "0047 SER 127.0.1.1 57000 \"$p\" 3" | nc -u 127.0.1.1 57000
done <Queries

Upvotes: 1

Related Questions