zsh-users
 help / color / mirror / code / Atom feed
From: Zach Riggle <zachriggle@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Commenting and large pipelines
Date: Wed, 11 May 2022 01:34:54 -0500	[thread overview]
Message-ID: <CAMP9c5mWOpVwTpDrrJ4JheYimwcTJRyuoWy9XDdMFJo6eQSb4w@mail.gmail.com> (raw)

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

Let's assume I have a series of large commands which need to be piped to
eachother.

Additionally, the pipeline itself must behave differently based on some
global flags or arguments.

There's a lot of way to make logic for this (e.g. functions), but one that
I've become a fan of is something like this below.

    cmd1=( some long command $FLAG bar baz )
    cmd2=( grep -E "$@" )
    cmd3=(
        grep
            -v # Don't match these
            -e 'fizz|buzz'
    )

    "${cmd1[@]}" | "${cmd2[@]}" | "${cmd3[@]}"

However, conditional logic makes this annoying and a bit opaque --
functions definitely excel here.

Another issue is that of adding comments to the arguments and the pipeline
itself -- lines that end with '\' cannot contain comments.  Functions
obviously can contain comments within themselves.

What I've discovered is that something like this works out pretty well...

    { printf "%s\n" a b foo c bar d fizz XfooX XbuzzX } |
    { grep -E 'foo|bar' } |
    {
        # If the user specified '--no-fizz-buzz', remove those entries
        if (( NO_FIZZ_BUZZ )); then
            grep -vE 'fizz|buzz'
        else
        # Pass through everything
            cat
        fi
    }

I have a few questions about this construct.

    1. Am I insane for doing this?
    2. In what ways is this a terrible idea?
    3. Is the use of {} better than ()?
    4. How much of a performance hit does this make, versus hand-writing a
different pipeline?
    5. Are there any ways to improve this?  For example, replacing 'cat' in
the default case.

Thanks for the attention, just curious what everybody thinks about this
abuse of pipelines and conditional logic.

*Zach Riggle*

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

             reply	other threads:[~2022-05-11  6:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-11  6:34 Zach Riggle [this message]
2022-05-11  7:13 ` Lawrence Velázquez
2022-05-12  6:15 ` 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=CAMP9c5mWOpVwTpDrrJ4JheYimwcTJRyuoWy9XDdMFJo6eQSb4w@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).