andrewchambers / janet-sh

Shorthand shell like functions for janet.
82 stars 6 forks source link

Redirect stderr to stdout? #17

Closed ianthehenry closed 1 year ago

ianthehenry commented 1 year ago

I would expect this to work, but it doesn't:

repl:1:> (use sh)
...
repl:2:> ($< sh -c `echo hi >&2` > [stderr stdout])
hi
""

(I know I can capture stderr by redirecting it to a buffer -- the $< is just to demonstrate that it didn't actually do the thing.)

Is there a way to redirect stderr to stdout?

andrewchambers commented 1 year ago

I thought this should work, seems like a bug potentially if it doesn't.

andrewchambers commented 1 year ago

I would also try swapping the stderr and stdout - I think its slightly counter intuitive - its essentially equivalent to a call to dup2 iirc.

ianthehenry commented 1 year ago

Yeah, swapping doesn't seem to make a difference:

repl:2:> ($< sh -c `echo hi >&2` > [stdout stderr])
hi
""
repl:3:> ($< sh -c `echo hi >&2` > [stderr stdout])
hi
""
ianthehenry commented 1 year ago

So the redirect was working correctly, and using sh/$ for example gave the correct behavior. The problem was that the implicit dup2 done by $< happens after this, so for a brief time stderr and stdout do refer to the same file, but the second call to dup2 causes stdout to point elsewhere. If I'm understanding this right. This appears to be easily fixed by #19.