Skip to main content
12 events
when toggle format what by license comment
Jul 1, 2018 at 9:53 comment added Tom Hale Any reason you didn't do exec >&"$fd- at the end to move the FD?
Aug 1, 2016 at 12:11 comment added alexey.e.egorov could you tell me where I can read more about such fd guarantees and caller responsibilities. I performed a search, but failed to find Linux-specific information. Thanks!
Aug 1, 2016 at 10:34 vote accept alexey.e.egorov
Jul 30, 2016 at 14:40 comment added Stéphane Chazelas @alexey.e.egorov, if upon start you script has some fds in (3..9) open, that's because your caller forgot to close them or set the close-on-exec flag on them. That's what I call a fd leak. Now, maybe the caller intended to pass those fds to you, so you can read and/or write data from/to them, but then you'd know about it. If you don't know about them, then you don't care, then you can close them freely (note that it just closes your script's process fd, not that of your caller).
Jul 30, 2016 at 14:06 comment added alexey.e.egorov Oh, understood. Hmmm... It seems quite strange. I only encountered the opposite case... Here, for example: unix.stackexchange.com/questions/295883. As you can see (in edit 2), rc.local leaves a bunch of opened fds with lower numbers to the script. Is it a bug? In terms of requirements violation.
Jul 30, 2016 at 14:05 history edited Stéphane Chazelas CC BY-SA 3.0
added 141 characters in body
Jul 30, 2016 at 13:25 comment added Stéphane Chazelas @alexey.e.egorov, no, you're looking at it backward. fds 3 to 9 are free to use (and it's up to you to manage them as you want) and are intended for that purpose. fds above 9 may be used by the shell internally and closing them could have nasty consequences. Most shells won't let you use them. bash will let you shoot yourself in the foot.
Jul 30, 2016 at 12:52 comment added alexey.e.egorov Bash 3.* doesnt support this feature, and this version is used in Centos 5, which is still supported and still used. And finding free descriptor and then eval "exec $i>&1" is a thing I would like to avoid, due to it cumbersomeness. Can I really rely on that fds above 9 would be free then?
Jul 30, 2016 at 6:54 comment added Stéphane Chazelas @alexey.e.egorov, see edit.
Jul 30, 2016 at 6:53 history edited Stéphane Chazelas CC BY-SA 3.0
added 1144 characters in body
Jul 30, 2016 at 1:34 comment added alexey.e.egorov Also, your code is wrong in a sense that fd 3 might be already taken, as it happens when a scrip run from a rc.local service, e.g. So you really shoud have used something like exec {FD}>&1 or something. But this is supported only in bash 4, which is really sad. So this isn't really portable.
Jul 29, 2016 at 18:26 history answered Stéphane Chazelas CC BY-SA 3.0