Deutsch   English   Français   Italiano  
<87tt5wnyt1.fsf@bsb.me.uk>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Ben Bacarisse <ben@bsb.me.uk>
Newsgroups: comp.unix.shell
Subject: Re: Create functional processing pipe (without eval)?
Date: Wed, 07 May 2025 17:57:46 +0100
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <87tt5wnyt1.fsf@bsb.me.uk>
References: <vveev6$dll3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 07 May 2025 18:57:54 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="bca44341774e55e4f209f0f280faf0a0";
	logging-data="1204844"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+ruLQfewI/d7+XMP1QpNFnarRnJmoGCEI="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:oq3sFtSIhx3AW1LG/+wY/wdGgco=
	sha1:KgdaXGb1fA5uaUb7liH90J0lNOM=
X-BSB-Auth: 1.399ff05d59230e8d12ba.20250507175746BST.87tt5wnyt1.fsf@bsb.me.uk

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.