Timeline for How do I close a named file descriptor without closing stdin/stdout?
Current License: CC BY-SA 4.0
20 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 11, 2023 at 18:19 | comment | added | ilkkachu |
@TiStrga, yes, but the error doesn't help much in the sense of getting the job done: the redirection still fails. E.g. f="foo bar.txt"; echo foo > $f just gives the error and nothing gets written to a file (not to foo bar.txt, nor foo nor bar.txt). In the case of exec, it's worse since unless you check that exec succeeds, the script just continues with the output going to the wrong place.
|
|
| Nov 11, 2023 at 18:13 | history | edited | ilkkachu | CC BY-SA 4.0 |
added 67 characters in body
|
| Nov 10, 2023 at 19:37 | comment | added | Ti Strga |
"assuming $gr is not IFS-splitted as you forgot the quotes and bash still does split+glob in redirections" Fortunately, bash will detect any IFS splits here; when discussing the word following a redirection operator, the man page now adds "If it expands to more than one word, bash reports an error." So no need for quotes; either the programmer got the $gr correct or it won't work in any case. :-)
|
|
| Apr 17, 2021 at 8:34 | history | edited | ilkkachu | CC BY-SA 4.0 |
makes more sense this way
|
| Apr 17, 2021 at 6:02 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 |
added 130 characters in body
|
| Apr 16, 2021 at 23:25 | comment | added | smac89 |
No I lied, it's not there. When I do exec >&${gr}-, $gr is gone as well. Oh well
|
|
| Apr 16, 2021 at 23:22 | comment | added | smac89 |
Actually I just got it, so that's the same as say I want to redirect stdout to stderr, and I do >&2, then if I append - to that command, it is exactly what I was doing in my question with exec >&${gr}- which causes stdout to be closed. Makes sense because when I check /dev/fd/ the original fd (${gr}) is still there. Whyy bash? Whyy??
|
|
| Apr 16, 2021 at 23:16 | vote | accept | smac89 | ||
| Apr 16, 2021 at 23:16 | comment | added | smac89 |
I'm still trying to wrap my head around exec <&$ec- and your explanation but I think you've answered my questions. I will just ask a new one later
|
|
| Apr 16, 2021 at 23:09 | comment | added | ilkkachu |
@smac89, in my Bash, exec ${ec}<&- gives an error (it doesn't recognize the ${ec} as part of the redirection.) But yah, {ec}<&- to close $ec.
|
|
| Apr 16, 2021 at 23:07 | comment | added | ilkkachu |
@smac89, not quite that either. Let's take something similar to the first one. exec {ec}< <(echo "echo bye"). Say ec gets the number 9, just to pick one. Now, fd 9 is connected to the process substitution. Now, don't do anything else with $ec, just run the exec <&$ec-, same as exec 1<&9-. That says to move 9 to 1 (stdin), so now 1 is connected to the process substitution and 9 gets closed. Then the shell reads input from 1, now the process substitution. It reads echo bye, runs that, gets an end-of-file, and exits.
|
|
| Apr 16, 2021 at 23:07 | comment | added | smac89 |
And..I just saw the other hidden easter egg. I was doing exec ${ec}<&-, instead of exec {ec}<&-. Man I need to take a break from this
|
|
| Apr 16, 2021 at 23:01 | comment | added | smac89 | I think I am starting to understand a bit now. So in my case, I was actually just closing stdin and stdout, and not the fd I was actually trying to close | |
| Apr 16, 2021 at 22:59 | history | edited | ilkkachu | CC BY-SA 4.0 |
deleted 112 characters in body
|
| Apr 16, 2021 at 22:52 | comment | added | ilkkachu |
@smac89 1>&7 would mean to fd 1 a copy of fd 7. 1>&- would close fd 1. I'm not at all sure what 1>&7- should be. Oh, should have read the manual, it moves the fd from 7 to 1.
|
|
| Apr 16, 2021 at 22:50 | comment | added | ilkkachu |
@smac89, oooh, right, it's /proc/$$/fd/, not /proc/self/fd/, so you're letting ruby "steal" the fd from the shell's fd list. That's... somewhat convoluted, but ok, I suppose it works.
|
|
| Apr 16, 2021 at 22:47 | comment | added | smac89 |
So you're saying that if I do something like exec 1>&7-, bash will simply parse this to mean "close fd 1", and just ignores the 7 in there? But without the -, it means, redirect fd 1 to fd 7? Seems like quite the jump in reasoning, but I guess it makes sense
|
|
| Apr 16, 2021 at 22:47 | history | edited | ilkkachu | CC BY-SA 4.0 |
added 250 characters in body
|
| Apr 16, 2021 at 22:45 | comment | added | smac89 |
Yea the sudo ruby <(echo "puts 'hello'") doesn't work. It is from this answer. The only way it worked was the way I had it in my question. I was going for a one-liner
|
|
| Apr 16, 2021 at 22:38 | history | answered | ilkkachu | CC BY-SA 4.0 |