Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: xargs with zsh function



On Sun, Jan 17, 2021 at 3:12 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> Can it be done?

The problem isn't the pipe, it's that xargs isn't zsh.

>  How do we pipe into a function?

That's the wrong question.  You're not piping into a function, you're
piping into a process (xargs) that turns its input lines into a set of
command-line arguments.

How about this:

$ autoload zargs
$ xzargs() { zargs -- "${(f)$(read -d '' -E)}" -- "$@" }
$ ... | xzargs my_function

That does use a lot of memory, reading the entire stdin and turning it
into a command line.

What you really want to do is write a nested shell loop that consumes
N lines, then runs my_function, then does that again until it runs out
of input; and pipe to that loop.  There are a bunch of ways to do
that.  E.g.:

... | while (true)
  do
    set --
    for i in {1..10}
    do
      read $i || break
    done
    my_function "$@"
    [[ $# -eq 10 ]] || break
  done




Messages sorted by: Reverse Date, Date, Thread, Author