From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 11456 invoked from network); 11 May 2022 06:36:03 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 11 May 2022 06:36:03 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1652250963; b=BqPNB14/DcxNKmsMwyUNWJjtGt/EdbeKBv3NK7kwnDtVdWFmrLE1Naq6oZ1e0sNcn5QKhAaNIW nn/TQykt6FoccnWjVwD1zIjgpD6odwP00whHdmI1NseDxmbKJcSp4zHvM+9/EVpbtEZ8sGQkrv G890Z1Tt3O+LWcfXm7kXVEvg2zC4DcYNtgVrrbkUgpuXlk55tzOvCTn0ebqGeIMvBGFDne/EB9 LIhe/vtlkPTZ6mAydBG6LA+N1UqRYBkFvbWShC2Q0KQKqJ8sLcmy70wWuZXQ4HbF28doWEqfpd q42np919lg6UtoYVE4uuO4v5d4KQNNDvnr5sn4ZxHS6DuQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-wr1-f44.google.com) smtp.remote-ip=209.85.221.44; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1652250963; bh=nEJBu4UXPsaWUcAXnGPyihTp8IesD9dYQELqjR0FUfk=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:To:Subject:Message-ID:Date:From:MIME-Version: DKIM-Signature:DKIM-Signature; b=GDBlGsaseDyxihm60ZgvV6bIlsyZo48eDuBtRgepf4q19aU7t5WWaFHB0XIzoWcocWIJp8e0re FE7hgjC2K5/vdjXKMjq9khvLNJL+ZAOdyImoYsbsS1SWXeaMHqdfsadJTBbbhH045Jc/Rfi3Fq hKna/jMVeZ1sXO4I2g/nPhJACbU1suvHOLt5HeuRxE61WpDUTUgLqWY8dZBpyAZTaVFVqoO88s Hv9/5OqlLcaLgbSgEAxzusWFjEGRVhgAQnUMD+g0AK+WjzYnB6FuIcNbXmaoKBdvv/kGcJp4S4 lSn+h7sAGBwsPKfGQ4kf2hF56V/9eHUHuD5gjnl8lKudPA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:To:Subject:Message-ID: Date:From:MIME-Version:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=KV4fVOWyUXzRkHtmgC+SHZTm0fAUnkphVCZAshKugjc=; b=gOaDLeVLxQuWQEeZ/VWvwFPh3d iv9iubmZEFS452TT3GHuNGut7xUd/4U9SNLdh/qirG49w5uMMnDfyvNFWIVxidvCnZ/gwWVCuQPaF vG8GqyuqA+KyCUpzjgw1qcfM7bP6xqWhcO2LLbgAtBomNf5eYoD783xQ7MmSENJUZdrJB9apQ5O90 1427cqo0PXFQRmqLYlt0X22cUVtWxMeWZF7EtLjaSMUmWTclKJ+O6s+QfDp/gAJHsv5yamalS4SAF iq2PbsbOtovIxArO9BlLl7xOxdp9HKBnCWm9vCOswtjQB8JLQH2iAdjgW58iSiDK55hC8h/ntVihj xmInNV9g==; Received: from authenticated user by zero.zsh.org with local id 1nofx4-0003S2-8U; Wed, 11 May 2022 06:36:02 +0000 Authentication-Results: zsh.org; iprev=pass (mail-wr1-f44.google.com) smtp.remote-ip=209.85.221.44; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-wr1-f44.google.com ([209.85.221.44]:38758) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1nofwA-0002kd-GW; Wed, 11 May 2022 06:35:06 +0000 Received: by mail-wr1-f44.google.com with SMTP id k2so1485890wrd.5 for ; Tue, 10 May 2022 23:35:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=KV4fVOWyUXzRkHtmgC+SHZTm0fAUnkphVCZAshKugjc=; b=fQGF1C5NC2krRwoczIl0wa+JO9un+ycUcgRx7m5FmlydkIv1FIra0ey5snQW0d/85c 3Tv+4uJNlEw4kX465/33FAdBMM8UBp8Gp6kIO8lY+mPxn5+nR37khpEizV0yvzomZ+wi x5jLta3kqSsiHi86vXkjtGRPCOdebaIPiIY7skCSV7D0NPTMESeBFQLEzmrpyuhGWbqe 6iSDgLsHW4AZdj7KJgmL/ubWlnps4LBk/wiQvQ+xYjz82/4ecPaxQpoG7Rw8BFOoEjh1 8yQ2H3uvmfXSDGfTzkNSVpZDgK4QsJOLGDDymfKJECmXQIx3MZelZxxHXWgrwhpsiu2O 0m/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=KV4fVOWyUXzRkHtmgC+SHZTm0fAUnkphVCZAshKugjc=; b=hiY6+wOENZaG3eVmWZ5u2dq6eGQSu3b7WCSqiFJN5dpOqVj0aKtfApHnQQFWp+gp/g n7eiVTlBMb5lExvnsCqUUF9qNF15z9Ltf9fK34VZH/F4462GbzMju4ax7scDGXzNaN61 slyOY1ovUd2l+VMgj0GwNI1qPglSOKmMSJbmn5IvBo7Y85LtTb5ig/0kqPytAjWjMYS3 BjFaDJGEsCx+y05PJ5Nrvyz0jaQt44cwLo7HrjR6ZfpJrypphrz/4lzzjfm8VA44Tpuh vjb6NIEOYCcuSQvcVh597x3wA0+J7jl0IxeHySlqfNCAYkAVi3zmNs9HS0LXgEMHHFv6 j5TA== X-Gm-Message-State: AOAM531AXNpB1NqZZ4slMJB4lwFrGujWNs4v/i1nYd/CtzscDzd9vted cCMvNhQ9Lc64DhaxQp4M0FnHZ3KeSkX6LYhmzxJukDlk6+0= X-Google-Smtp-Source: ABdhPJz7o7e+77htTQJpNk447l++io4JHbG87CJKMbBmShGKXV6v1R0uCRQ5VNUfKy6mrZdDtuFVKt6CHWpi/pPyKtM= X-Received: by 2002:adf:d1c7:0:b0:20c:4d5f:e57f with SMTP id b7-20020adfd1c7000000b0020c4d5fe57fmr21250326wrd.453.1652250905545; Tue, 10 May 2022 23:35:05 -0700 (PDT) MIME-Version: 1.0 From: Zach Riggle Date: Wed, 11 May 2022 01:34:54 -0500 Message-ID: Subject: Commenting and large pipelines To: Zsh Users Content-Type: multipart/alternative; boundary="0000000000004e10d605deb6a4a5" X-Seq: 27752 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: --0000000000004e10d605deb6a4a5 Content-Type: text/plain; charset="UTF-8" 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* --0000000000004e10d605deb6a4a5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Let's assume I have a series of large commands which n= eed to be piped to eachother.

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

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

=C2=A0 =C2=A0 cm= d1=3D( some long command $FLAG bar baz )
=C2=A0 =C2=A0 cmd2=3D( grep -E = "$@" )
=C2=A0 =C2=A0 cmd3=3D(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 g= rep
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -v # Don't match these=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -e 'fizz|buzz'
=C2= =A0 =C2=A0 )

=C2=A0 =C2=A0 "${cmd1[@]}" | "${cmd2[@]}= " | "${cmd3[@]}"

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

Anot= her issue is that of adding comments to the arguments and the pipeline itse= lf -- lines that end with '\' cannot contain comments.=C2=A0 Functi= ons obviously can contain comments within themselves.

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

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

I have a few quest= ions about this construct.

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

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

Zach Riggle

--0000000000004e10d605deb6a4a5--