zsh-users
 help / color / mirror / code / Atom feed
* Temporary redirection of named file descriptors
@ 2021-10-19 19:49 Zach Riggle
  2021-10-20 17:36 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Zach Riggle @ 2021-10-19 19:49 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1494 bytes --]

Hello all!

TL;DR: How can I redirect a named file descriptor (to e.g. /dev/null), but
only for one statement?
Neither of these work, and Section 7 doesn't provide an example.

    somefunc {myfd}>/dev/null
    somefunc $myfd>/dev/null

== More context and background ==

I have a neat little library of commonly-used functions, "zshlib", that
I've got set up as an autoloadable module (technically an auto-loaded
function, but it exposes 50+ functions to the global namespace and I'm more
familiar with Python nomenclature).

Scripts which use "zshlib" load it at the very top of the script.  The
first thing thing "zshlib" does is create a named file descriptor:

    exec {zshlog}>&2

This is done for lots of reasons, mainly the "run a command but print it
out and properly escaped so the user knows what is taking so long"
functions.  The executed command may write to stdout or stderr, and I
wanted to ensure my library does not pollute those streams, or prevent them
from being grepped / redirected / filtered / etc (as much as possible,
anyway).

This is generally achieved per-statement or for a statement-list.

    >$zshlog command echo foo bar

This is all great, but occasionally I want to silence the output from
"zshlib".  I assumed either of these syntaxes would work:

    somefunc {zshlog}>/dev/null
    somefunc $zshlog>/dev/null

The first fails with "can't clobber parameter zshlog containing file
descriptor 11".
The second simply uses $zshlog as a parameter to somefunc.

[-- Attachment #2: Type: text/html, Size: 2068 bytes --]

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

* Re: Temporary redirection of named file descriptors
  2021-10-19 19:49 Temporary redirection of named file descriptors Zach Riggle
@ 2021-10-20 17:36 ` Bart Schaefer
  2021-10-20 17:38   ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2021-10-20 17:36 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Tue, Oct 19, 2021 at 12:50 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> TL;DR: How can I redirect a named file descriptor (to e.g. /dev/null), but only for one statement?

The short answer is that you can't do what you're trying to do in the
way you're trying to do it.  The {myfd}> syntax is defined to create a
new value in $myfd, and the N> syntax can only be used for N from 0
through 9, so there's no way to use the latter to re-redirect a
descriptor created by the former (which are all numbered more than
10).

So you have to do it something like this (example using an anonymous function):

function {
  local zshlog
  : {zshlog}>/dev/null
  somefunc "$@"
  {zshlog}>&-
} ... args for somefunc ...

> This is generally achieved per-statement or for a statement-list.
>
>     >$zshlog command echo foo bar

I presume that should be >&$zshlog


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

* Re: Temporary redirection of named file descriptors
  2021-10-20 17:36 ` Bart Schaefer
@ 2021-10-20 17:38   ` Bart Schaefer
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2021-10-20 17:38 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Wed, Oct 20, 2021 at 10:36 AM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
>   {zshlog}>&-

That's a typo, should be

  : {zshlog}>&-

("exec" also works instead of ":" if that's clearer)


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

end of thread, other threads:[~2021-10-20 17:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-19 19:49 Temporary redirection of named file descriptors Zach Riggle
2021-10-20 17:36 ` Bart Schaefer
2021-10-20 17:38   ` 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).