zsh-users
 help / color / mirror / code / Atom feed
From: Zach Riggle <zachriggle@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Temporary redirection of named file descriptors
Date: Tue, 19 Oct 2021 14:49:57 -0500	[thread overview]
Message-ID: <CAMP9c5nFP08KRUmuMRgVzKySNqF0nc-aT37WSBC-SSNDhm3nKQ@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2021-10-19 19:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-19 19:49 Zach Riggle [this message]
2021-10-20 17:36 ` Bart Schaefer
2021-10-20 17:38   ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMP9c5nFP08KRUmuMRgVzKySNqF0nc-aT37WSBC-SSNDhm3nKQ@mail.gmail.com \
    --to=zachriggle@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).