Shell – Problem with getting interactive input in while read loop

inputio-redirectionpipereadshell

I was doing something like the following minimal example:

#! /bin/sh

# $1 = file containing list of files to sync
# $2 = source dir ; $3 = target dir

cat "$1" | while read f
do
    cp -i "$2"/"$f" "$3"/"$f"
done

I find that cp -i doesn't wait for my input at all and the loop just runs to the end. Why, and what can be done to fix the situation?

Best Answer

Digging around here, I understood from https://unix.stackexchange.com/a/56877/54067 (where both the question and answer are worded differently and the problem is not related to interactive input) that the reason for the problem is that the cp -i expects the user to give the interactive input confirmation via stdin but in the cat | while read loop stdin is busy with the pipe and so the following line(s) from the input file is/are chewed up as user input.

So the solution is also similar to that advised at the other question -- to pipe the content through a different file descriptor than stdin:

while read f <&3
do
    cp -i "$2"/"$f" "$3"/"$f"
done 3< "$1"

Note the <&3 and 3< "$1" at the top and bottom of the loop.

Thanks to @StéphaneChazelas for the answer in the other q. (I upvoted it.)

Further, posting this here obviously I'd like to know if there are any other solutions for interactive input other than this.

Also note that while using a for loop is a possible as in the other q, I'm worrying that the $(cat "$1") in for f in "$(cat "$1")" will use up unnecessary memory by expanding the file in place if it is a very big one. OTOH the pipe should handle reading line by line IIUC. So I don't think using for is an efficient solution. I'd like comments on this as well.

Related Question