Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect both stdout and stderr of a process to the same stream #5344

Closed
carlobaldassi opened this issue Jan 9, 2014 · 8 comments
Closed
Assignees
Labels
io Involving the I/O subsystem: libuv, read, write, etc.
Milestone

Comments

@carlobaldassi
Copy link
Member

Currently, there is no operator to redirect both stderr and stdout to the same target (in bash that would be &>), or to collapse them before redirecting (in bash: 2>&1). I think we should come up with some general scheme for this kind of things (at some point there was an idea about using dictionaries to represent redirections), but even with what we have I was expecting to be able to do this:

open("out.txt", "w") do f
    run(`ls exists.txt doesnotexsist.txt` |> f .> f)
end

but this fails since it only redirects stdout.
My current workaround is this ugly hack:

function open_twice(func, fn, args...)
    open(fn, args...) do f1
        f2 = fdio(int32(Base.dup(RawFD(fd(f1)))), true)
        try
            func(f1, f2)
        finally
            close(f2)
        end
    end
end

open_twice("lsout.txt", "w") do f1,f2
    run(`ls exists.txt doesnotexsist.txt` |> f1 .> f2)
end

Could the first attempt be made to work instead? I tried looking at the code but as soon as it goes into libuv I'm lost. So I could make a patch and put the ugly hack in place if stdout and stderr are the same, but I suppose there is a better alternative.

@StefanKarpinski
Copy link
Member

I also consider using the .> operator no good – it's just as much of a pun as using > was.

@StefanKarpinski
Copy link
Member

I've considered this:

`head -c 1000 /dev/random` |> (1,f) |> (2,f)

Still needs to be made to work when the same thing is redirected to twice.

@carlobaldassi
Copy link
Member Author

I opened a separate issue (#5349) to track the syntax discussion, so that this one can be specific to the bug of not being able to redirect two streams into one with the current interface (of course, if the other issue is solved first, this one may be closed; I'd be surprised though).

@vtjnash
Copy link
Member

vtjnash commented Jan 12, 2014

The following is supposed to work (@loladiro), but line 226 of libuv/src/unix/process.c is too greedy at calling close:

open("lsout.txt", "w") do f1
    run(`ls exists.txt doesnotexsist.txt`, DevNull, f1, f1)
end

@IainNZ
Copy link
Member

IainNZ commented Mar 4, 2014

Would it be impertinent of me to suggest a 0.4 milestone for this one?

@vtjnash
Copy link
Member

vtjnash commented Mar 4, 2014

I like putting 0.3 on stuff and see what sticks

@IainNZ
Copy link
Member

IainNZ commented Mar 4, 2014

haha, my man 👍

@vtjnash vtjnash self-assigned this Mar 12, 2014
@vtjnash vtjnash added this to the 0.3 milestone Mar 12, 2014
@vtjnash
Copy link
Member

vtjnash commented Mar 12, 2014

I think I found multiple more related bugs in upstream libuv (and thus Julia's libuv) while fixing this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
io Involving the I/O subsystem: libuv, read, write, etc.
Projects
None yet
Development

No branches or pull requests

4 participants