DEV Community

Discussion on: Shell Pipe Wrapper Functions

Collapse
 
taikedz profile image
Tai Kedzierski • Edited

From what I can see here, this is what the xargs command is for..

cmd1 | xargs cmd2 will run cmd1, its output is then piped through and xargs will take care of running cmd2 on every line of piped data.

The rm command would be written as

path_gen_cmd | xargs rm -i
Enter fullscreen mode Exit fullscreen mode

which includes the built-in interactive element of prompting the user for each item.

Collapse
 
thefluxapex profile image
Ian Pride • Edited

But my prototype method allows all kinds of extra logic processing and I don't like using xargs.

Collapse
 
taikedz profile image
Tai Kedzierski • Edited

If personal preference, fair, but at that point it's no longer "generic" 😉

Seizing the input from keyboard whilst within a pipe by using read -u 1 merits a more significant callout though, that's handy to know...!

Thread Thread
 
thefluxapex profile image
Ian Pride • Edited

Fair enough on the `no longer "generic", but I usually don't add too much extra and tend to add these functions in my hybrid script/function method so it's a bit more portable for me. Certainly preference though, was just providing alternatives. Appreciate the feedback.