I believe this is supported in the Bourne shell:
exec 3<doc.txt
while read LINE <&3
do
stuff-done-to-$LINE
# the next two lines could be replaced by: read -p "Enter input: " INPUT
echo "Enter input:"
read INPUT
stuff-done-to-$INPUT
done < infile
Input is alternated between the file and the user. In fact, this would be a neat way to issue a series of prompts from a file.
This redirects the file "infile" to the file descriptor number 3 from which the first read
gets its input. File descriptor 0 is stdin
, 1 is stdout
and 2 is stderr
. You can use other FDs along with them.
I've tested this on Bash and Dash (on my system sh is symlinked to dash).
Of course it works. Here's some more fun:
exec 3<doc1.txt
exec 4<doc2.txt
while read line1 <&3 && read line2 <&4
do
echo "ONE: $line1"
echo "TWO: $line2"
line1=($line1) # convert to an array
line2=($line2)
echo "Colors: ${line1[0]} and ${line2[0]}"
done
This alternates printing the contents of two files, discarding the extra lines of whichever file is longer.
ONE: Red first line of doc1
TWO: Blue first line of doc2
Colors: Red and Blue
ONE: Green second line of doc1
TWO: Yellow second line of doc2
Colors: Green and Yellow
Doc1 only has two lines. The third line and subsequent lines of doc2 are discarded.