Re: Create functional processing pipe (without eval)?

Liste des GroupesRevenir à cu shell 
Sujet : Re: Create functional processing pipe (without eval)?
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.unix.shell
Date : 07. May 2025, 17:57:46
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87tt5wnyt1.fsf@bsb.me.uk>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13)
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:

I'm pondering about creating a functional processing pipe, depending
on program parameters, and whether that's possible to achieve without
using 'eval'.
>
Say, the program is called "filter" and may accept 0..N parameters and
depending on the set of parameters the respective pipe functionality
shall be defined like
>
  filter               =>  cat
  filter p1            =>  cat | func p1
  filter p1 p2         =>  cat | func p1 | func p2
  filter p1 p2 ... pN  =>  cat | func p1 | func p2 | ... | func pN
>
where "func" is working as filter and accepts exactly one parameter.

You don't really need the initial cat except when there are no
arguments.  Using this fact, and provided you don't mind an extra cat
command at the end of the pipe, does this do what you want:

  #!/bin/bash
  
  function recurse
  {
      if [[ $# -ge 1 ]]
      then grep "$1" | (shift; recurse "$@")
      else cat
      fi
  }

  recurse "$@"

(I'm using grep and the filer here because it permits simple testing.)

I think one could remove the final cat command, but I'm in hurry to
catch the shops!

--
Ben.

Date Sujet#  Auteur
7 May 25 * Create functional processing pipe (without eval)?10Janis Papanagnou
7 May 25 +* Re: Create functional processing pipe (without eval)?2Kaz Kylheku
7 May 25 i`- Re: Create functional processing pipe (without eval)?1Janis Papanagnou
7 May 25 +* Re: Create functional processing pipe (without eval)?3Lars Poulsen
7 May 25 i`* Re: Create functional processing pipe (without eval)?2Janis Papanagnou
7 May 25 i `- Re: Create functional processing pipe (without eval)?1Janis Papanagnou
7 May 25 `* Re: Create functional processing pipe (without eval)?4Ben Bacarisse
7 May 25  `* Re: Create functional processing pipe (without eval)?3Janis Papanagnou
7 May 25   `* Re: Create functional processing pipe (without eval)?2Christian Weisgerber
7 May 25    `- Re: Create functional processing pipe (without eval)?1Janis Papanagnou

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal