Sujet : Re: Create functional processing pipe (without eval)?
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.unix.shellDate : 07. May 2025, 20:11:30
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vvgb94$16oel$1@dont-email.me>
References : 1 2
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 07.05.2025 18:57, Ben Bacarisse wrote:
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.
Yes. As far as I see we need some terminator at one end of the pipe
if we have that in some construction context ('eval'-.string or pipe).
I used it at the front of the pipeline because it allows for a simple
logic; my 'eval'-based logic (that I wanted to avoid) looks like this
pipe="cat"
for p in "$@"
do
pipe="${pipe} | func '${p}'"
done
eval "${pipe}"
and it reflects the four samples we see above (with 'cat' in front).
Using this fact, and provided you don't mind an extra cat
command at the end of the pipe,
I don't mind an extra 'cat'. Whether its at the front or rear is not
too different (as far as I see).
does this do what you want:
#!/bin/bash
function recurse
{
if [[ $# -ge 1 ]]
then grep "$1" | (shift; recurse "$@")
else cat
fi
}
recurse "$@"
This is very nice! - It definitely works for me and does what I want.
Thanks!
(I'm using grep and the filer here because it permits simple testing.)
Did you mean "as the filter"? (Otherwise please explain the "filer".)
I think one could remove the final cat command, but I'm in hurry to
catch the shops!
I certainly need a "transparent" (idempotent) function if there are no
arguments provided, so a 'cat' is what I need in that case. (An empty
output would definitely be wrong for me.) And the unnecessary 'cat' at
the pipe end(s) is an acceptable unimportant degradation (for me). But
if you have an idea how to remove it... :-)
Janis