Shell Scripting: while read line from file + user input inside while loop
by ondoho from LinuxQuestions.org on (#53BGV)
I have a fairly standard while loop that reads strings from a file like this:
Code:while read -r line; do
read -r -p "Give me some input " answer
if [[ "$answer" ... etc.
done <fileThis (nested reads from stdin) clearly doesn't work as desired.
I tried searching for a solution for this specific problem but did not see anything that clearly mentions it.
I did come up with this:
Code:while read -r -u3 line; do
read -r -p "Give me some input " answer
if [[ "$answer" ... etc.
done 3<fileSo, I open a file descriptor 3 and read the lines from that, while user input goes through stdin.
This works as desired.
But - and as I said, I could not formulate the search terms to find any clear answers - is it OK to do this? Is it the proper way? Any pitfalls there?
PS: I use bash.


Code:while read -r line; do
read -r -p "Give me some input " answer
if [[ "$answer" ... etc.
done <fileThis (nested reads from stdin) clearly doesn't work as desired.
I tried searching for a solution for this specific problem but did not see anything that clearly mentions it.
I did come up with this:
Code:while read -r -u3 line; do
read -r -p "Give me some input " answer
if [[ "$answer" ... etc.
done 3<fileSo, I open a file descriptor 3 and read the lines from that, while user input goes through stdin.
This works as desired.
But - and as I said, I could not formulate the search terms to find any clear answers - is it OK to do this? Is it the proper way? Any pitfalls there?
PS: I use bash.