Bash: reading input within while read loop doesn't work

13

2

Reading input within a while read loop does not seem to work

while read line
do
 echo "get some input from the user"
 read response
done < some_file.txt

execution does not pause like it would had the read been outside the loop. Why is this? Is there a workaround for reading input within a while read loop?

well actually

Posted 2012-05-07T19:36:21.157

Reputation: 483

Answers

13

The problem is that both read line and read response expects (and gets) data from stdin.
This question on SO explains some of it with a link to even more information.

tl;dr
The accepted answer suggests:

Read from the controlling terminal device: read input </dev/tty

Nifle

Posted 2012-05-07T19:36:21.157

Reputation: 31 337

16

let the inner read command use stdin, and use a different file descriptor for the while loop

while read -u 3 line; do
  read -p "get some input from the user" response
done 3< some_file.txt

glenn jackman

Posted 2012-05-07T19:36:21.157

Reputation: 18 546

1

Nifle has it exactly right. When you're using multiple terminals, however, you need to be specific.

For those of you coming from google, congratulations on finding this page. If you need to do any user input during a while read loop (this includes rm -i, read, or anything else), you can specify which input pipe to use.

Here's a fragment of this solution that I used:

#in declarations
thistty=$(tty)

lsuser -R LDAP -a home pgrp ALL 2>/dev/null | while read line
do
   homedir=$(echo $homedir | awk -F= '{print $2}')
   sudo rm -ir "$homedir" < $thistty
done

bgStack15

Posted 2012-05-07T19:36:21.157

Reputation: 1 644

1

Thanks Nifle! And also thanks to bgStack. After searching for hours i finally got the answer! Great one!! I used "echo $(tty)" to detect my terminal path or you just take it as variable. For me it was a other use case. U was reading a file and would like to confirm the execution. Maybe the example below helps some one else out.

#!/bin/bash

export terminal=$(tty)

cat file | while read val1 val2
do
   while true; 
            do
              read -p "would you like to XYZ" yn
              case $yn in
                        [Yy]* )     echo "# Move $val1 to $val2        #";break;;
                        [Nn]* )     echo "#---------no action----------#";break;;
                        * )         echo "# Please answer yes or no.   #";;
              esac
            done < $terminal
done

Micha

Posted 2012-05-07T19:36:21.157

Reputation: 11

In my case I had while reading from a pipe... and then I used redirection to read from stdin: read something < %terminal. – eftshift0 – 2019-10-25T02:52:01.403