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
while read f <&3
cp -i "$2"/"$f" "$3"/"$f"
done 3< "$1"
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.