zsh-users
 help / color / mirror / code / Atom feed
* Redirect a specific file descriptor to a pipe?
@ 2017-03-10  0:21 Nathan Dorfman
  2017-03-10  2:04 ` Bart Schaefer
       [not found] ` <170309180417.ZM14034__34042.4779606566$1489111700$gmane$org@torch.brasslantern.com>
  0 siblings, 2 replies; 7+ messages in thread
From: Nathan Dorfman @ 2017-03-10  0:21 UTC (permalink / raw)
  To: zsh-users

Hi,

It's easy to create an arbitrary file descriptor and redirect it to a
file, e.g. 3>foo. Is there any way to redirect it to a pipe instead of
a file?

For example (assuming Linux),

strace -o /dev/fd/3 ./a.out > out.log 2> err.log 3>XXX

Instead of file XXX, I'd like to send fd 3 to |less. Is it possible?

Many thanks.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Redirect a specific file descriptor to a pipe?
  2017-03-10  0:21 Redirect a specific file descriptor to a pipe? Nathan Dorfman
@ 2017-03-10  2:04 ` Bart Schaefer
       [not found] ` <170309180417.ZM14034__34042.4779606566$1489111700$gmane$org@torch.brasslantern.com>
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2017-03-10  2:04 UTC (permalink / raw)
  To: Nathan Dorfman, zsh-users

On Mar 9,  5:21pm, Nathan Dorfman wrote:
}
} strace -o /dev/fd/3 ./a.out > out.log 2> err.log 3>XXX
} 
} Instead of file XXX, I'd like to send fd 3 to |less. Is it possible?

You just need this:

strace -o /dev/fd/3 ./a.out > out.log 2> err.log 3>&1 | less


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Redirect a specific file descriptor to a pipe?
       [not found] ` <170309180417.ZM14034__34042.4779606566$1489111700$gmane$org@torch.brasslantern.com>
@ 2017-03-10 17:22   ` Stephane Chazelas
  2017-03-10 18:51     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Stephane Chazelas @ 2017-03-10 17:22 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Nathan Dorfman, zsh-users

2017-03-09 18:04:17 -0800, Bart Schaefer:
> On Mar 9,  5:21pm, Nathan Dorfman wrote:
> }
> } strace -o /dev/fd/3 ./a.out > out.log 2> err.log 3>XXX
> } 
> } Instead of file XXX, I'd like to send fd 3 to |less. Is it possible?
> 
> You just need this:
> 
> strace -o /dev/fd/3 ./a.out > out.log 2> err.log 3>&1 | less
[...]

No quite. There are two issues here, one zsh specific:

cmd > file | cmd2

In zsh (with multios on by default) is special and redirects
stdout both to file and cmd2 (and the 3>&1 would also redirect
fd 3 to both).

In Bourne-like syntax, you'd also need to change the order:

strace -o /dev/fd/3 ./a.out 3>&1 > out.log 2> err.log | less

That is, redirect the fd 3 to the same thing as fd 1 at the time
that fd 1 was the pipe, *and then* redirect fd 1 to out.log.

In zsh, to avoid the multios effect, you can do:

{strace -o /dev/fd/3 ./a.out 3>&1 > out.log 2> err.log} | less

or disable multios (set +o multios).

-- 
Stephane


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Redirect a specific file descriptor to a pipe?
  2017-03-10 17:22   ` Stephane Chazelas
@ 2017-03-10 18:51     ` Bart Schaefer
  2017-03-10 23:32       ` Nathan Dorfman
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2017-03-10 18:51 UTC (permalink / raw)
  To: zsh-users

On Mar 10,  5:22pm, Stephane Chazelas wrote:
}
} > strace -o /dev/fd/3 ./a.out > out.log 2> err.log 3>&1 | less
} [...]

Indeed, I've got the 3>&1 in the wrong place.  Sorry about that.

} In zsh, to avoid the multios effect, you can do:
} 
} {strace -o /dev/fd/3 ./a.out 3>&1 > out.log 2> err.log} | less
} 
} or disable multios (set +o multios).

With multios and without the braces, out.log gets the a.out output
and less gets both the strace output and the a.out output, which is
actually what I meant to do (but might not be exactly what Nathan
wanted, in retrospect).  Thanks for the correction.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Redirect a specific file descriptor to a pipe?
  2017-03-10 18:51     ` Bart Schaefer
@ 2017-03-10 23:32       ` Nathan Dorfman
  2017-03-11  0:17         ` Mikael Magnusson
  2017-03-11  2:24         ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: Nathan Dorfman @ 2017-03-10 23:32 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

On Fri, Mar 10, 2017 at 11:51 AM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
> } In zsh, to avoid the multios effect, you can do:
> }
> } {strace -o /dev/fd/3 ./a.out 3>&1 > out.log 2> err.log} | less
> }
> } or disable multios (set +o multios).
>
> With multios and without the braces, out.log gets the a.out output
> and less gets both the strace output and the a.out output, which is
> actually what I meant to do (but might not be exactly what Nathan
> wanted, in retrospect).  Thanks for the correction.

Thanks guys, this does actually answer my question, and makes sense
now that I think about.

I still think it might be useful to be able to pipe any fd, not just
stdout, but I don't know if I can contrive a great example. Perhaps we
can't redirect stdout because it's a curses program that needs direct
terminal access, but we want to do something like this:

strace -o /dev/fd/3 mutt 3>XXX

Where instead of XXX, we want to send fd 3 to |xz or |ssh?

Feel free to ignore me if you think I'm going off the deep end :)

-nd.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Redirect a specific file descriptor to a pipe?
  2017-03-10 23:32       ` Nathan Dorfman
@ 2017-03-11  0:17         ` Mikael Magnusson
  2017-03-11  2:24         ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Mikael Magnusson @ 2017-03-11  0:17 UTC (permalink / raw)
  To: Nathan Dorfman; +Cc: Bart Schaefer, Zsh Users

On Sat, Mar 11, 2017 at 12:32 AM, Nathan Dorfman <ndorfman@gmail.com> wrote:
> On Fri, Mar 10, 2017 at 11:51 AM, Bart Schaefer
> <schaefer@brasslantern.com> wrote:
>> } In zsh, to avoid the multios effect, you can do:
>> }
>> } {strace -o /dev/fd/3 ./a.out 3>&1 > out.log 2> err.log} | less
>> }
>> } or disable multios (set +o multios).
>>
>> With multios and without the braces, out.log gets the a.out output
>> and less gets both the strace output and the a.out output, which is
>> actually what I meant to do (but might not be exactly what Nathan
>> wanted, in retrospect).  Thanks for the correction.
>
> Thanks guys, this does actually answer my question, and makes sense
> now that I think about.
>
> I still think it might be useful to be able to pipe any fd, not just
> stdout, but I don't know if I can contrive a great example. Perhaps we
> can't redirect stdout because it's a curses program that needs direct
> terminal access, but we want to do something like this:
>
> strace -o /dev/fd/3 mutt 3>XXX
>
> Where instead of XXX, we want to send fd 3 to |xz or |ssh?
>
> Feel free to ignore me if you think I'm going off the deep end :)

you can in theory do this

whatever_command 5> >(pipe command here) 7> >(some other pipe here)

but now that i'm testing it, it seems to not complete when it should, eg
strace -o /dev/fd/7 =true 7>&1 | cat
immediately returns but
strace -o /dev/fd/7 =true 7> >(cat)
just hangs after printing the output

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Redirect a specific file descriptor to a pipe?
  2017-03-10 23:32       ` Nathan Dorfman
  2017-03-11  0:17         ` Mikael Magnusson
@ 2017-03-11  2:24         ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2017-03-11  2:24 UTC (permalink / raw)
  To: zsh-users

On Mar 10,  4:32pm, Nathan Dorfman wrote:
}
} I still think it might be useful to be able to pipe any fd, not just
} stdout, but I don't know if I can contrive a great example.

It depends on what your intentions are.  The definition of a pipeline
in shell syntax is that the standard output of the left side connects
to the standard input of the right side.  So if you want to use a pipe,
you have to make the right thing *be* stdout, regardless of which other
descriptor it might have started out to be.

So when I talk about intentions: Where do you expect stdout of the left
side to go if some other descriptor is feeding the pipe?

} Perhaps we
} can't redirect stdout because it's a curses program that needs direct
} terminal access, but we want to do something like this:
} 
} strace -o /dev/fd/3 mutt 3>XXX
} 
} Where instead of XXX, we want to send fd 3 to |xz or |ssh?

In that example you want "mutt" to remain the foreground process (it
can't share the terminal with xz or ssh).  So you have to run xz in
the background.  That is what process substitution is for:

    strace -o /dev/fd/3 mutt 3>>(xz)

This puts xz in the background with its input connected to fd 3 and
it's output still connected to the terminal, while mutt stays in the
foreground with stdin + stdout unchanged.

It's up to you (or to xz) to make sure the subshell doesn't attempt to
spew something onto the terminal mutt is using.

On Mar 11,  1:17am, Mikael Magnusson wrote:
}
} whatever_command 5> >(pipe command here) 7> >(some other pipe here)
}
} but now that i'm testing it, it seems to not complete when it should

It's not hung, >(some other pipe here) is in the background so its
output has probably been printed below the prompt.  Try

strace -o /dev/fd/7 =true 7> >(cat); wait

which will wait for >(cat) to finish before printing another prompt [it
is a relatively recent change to zsh that you can "wait" for process
substitution background jobs].


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-03-11  2:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-10  0:21 Redirect a specific file descriptor to a pipe? Nathan Dorfman
2017-03-10  2:04 ` Bart Schaefer
     [not found] ` <170309180417.ZM14034__34042.4779606566$1489111700$gmane$org@torch.brasslantern.com>
2017-03-10 17:22   ` Stephane Chazelas
2017-03-10 18:51     ` Bart Schaefer
2017-03-10 23:32       ` Nathan Dorfman
2017-03-11  0:17         ` Mikael Magnusson
2017-03-11  2:24         ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).