LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-12-2020, 01:54 AM   #1
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,557
Blog Entries: 9

Rep: Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068
Shell Scripting: while read line from file + user input inside while loop


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 <file
This (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<file
So, 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.
 
Old 05-12-2020, 02:04 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,624

Rep: Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756
yes, in general it is ok.
In your first example stdin is processed by both read commands. This is a common mistake (while reading stdin and something inside wanted to use too). If you wish to separate them you need to use different file descriptors (since you cannot have more than one stdin).
There are ways to find the first free fd, sometimes 3 is already in use.
 
1 members found this post helpful.
Old 05-12-2020, 02:15 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,392

Rep: Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624
Code:
while read -r -u3 line; do
Yes that's the solution for bash,ksh,zsh.
The all shells solution is
Code:
while read -r line <&3; do
 
2 members found this post helpful.
Old 05-14-2020, 01:15 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,557

Original Poster
Blog Entries: 9

Rep: Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068
Thanks!
Is that the general recommendation for getting in user input in a loop that reads in something else?
I have the feeling (because my web searches were fruitless which doesn't happen often) that I'm missing something obvious here.

The only thing I could think of was to read the file into an array and use 'for element in "{array[@]}"; do ...', very cumbersome!

Quote:
Originally Posted by pan64 View Post
There are ways to find the first free fd, sometimes 3 is already in use.
Interesting! Care to elaborate?
 
Old 05-14-2020, 01:57 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,624

Rep: Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756
see man bash:
Quote:
Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {varname}. In this case, for each redirection operator except >&- and <&-, the shell will allocate a file descriptor greater than or equal to 10 and assign it to varname.
or for example here: https://stackoverflow.com/questions/...ile-descriptor

and a relatively simple but useful example: https://stackoverflow.com/questions/...riptor-in-bash
 
2 members found this post helpful.
Old 05-14-2020, 03:20 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,557

Original Poster
Blog Entries: 9

Rep: Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068
Thanks again, named FDs it will be from now on.
 
Old 05-15-2020, 12:13 PM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,624

Rep: Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756
Quote:
Originally Posted by ondoho View Post
Thanks again, named FDs it will be from now on.
Actually that is not named FD, but a variable containing a number (and used as FD). But it is irrelevant.

Code:
$ exec {fff}>/tmp/aaa
$ echo $fff
10
$ echo dfgfd>&10
$ cat /tmp/aaa
dfgfd
 
Old 05-16-2020, 03:13 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,557

Original Poster
Blog Entries: 9

Rep: Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068Reputation: 4068
Quote:
Originally Posted by pan64 View Post
Actually that is not named FD, but a variable containing a number (and used as FD). But it is irrelevant.
A variable is a name.
Unless there's also another thing explicitely called "named filedescriptors", or you assign some strictly defined action to the verb "name", it is indeed irrelevant.

So this line:
Code:
exec {FD}>test.txt
automatically finds the first unused file descriptor on fairly recent (>3) bash.

About closing the FD after done with it: doesn't that happen automatically when the script exits, like with most things used inside a script?
 
Old 05-16-2020, 03:21 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,624

Rep: Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756
Quote:
Originally Posted by ondoho View Post

About closing the FD after done with it: doesn't that happen automatically when the script exits, like with most things used inside a script?
This is more or less true. There can be problems when it has child process[es] which inherited these descriptors.
But in general yes, they are automatically closed.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"while" loop does not read file line by line mackowiakp Linux - Newbie 15 12-04-2017 03:51 AM
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
How can I read a file line by line and add it to a loop in another file? astroumut Linux - Newbie 7 08-24-2009 04:37 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration