Live data from Hacker News

An Opinionated Guide to Xargs

oilshell.org

31–40 of 135 posts

Re: An Opinionated Guide to Xargs

#31
post #23

awk '{ print your_command }' | bash Never can remember all the -I stuff around xargs

This is like the sed|bash anti-pattern mentioned in the original post, and quoted in the appendix on shell injection.

I wouldn't say "never use it", but I would hesitate to ever put it in a script, vs. doing a one-off at the command line.

Re: An Opinionated Guide to Xargs

#33
post #11

Wanting verbose logging from xargs, years ago I wrote a script called `el` (edit lines) that basically does `xargs -0` with logging. https://github.com/westurner/dotfiles/blob/develop/scripts/e... It turns out that e.g. -print0 and -0 are the only safe way: line endings aren't escaped: find . -type f -print0 | el -0 --each -x echo GNU Parallel is a much better tool: https://en.wikipedia.org/wiki/GNU_parallel

(author here) Hm I don't see either of these points because: GNU xargs has --verbose which logs every command. Does that not do what you want? (Maybe I should mention its existence in the post) xargs -P can do everything GNU parallel do, which I mention in the post. Any counterexamples? GNU parallel is a very ugly DSL IMO, and I don't see what it adds. -- edit: Logging can also be done with by recursively invoking sh…

[deleted]

Re: An Opinionated Guide to Xargs

#34

I frequently find myself reaching for this pattern instead of xargs: do_something | ( while read -r v; do . . . done ) I’ve found that it has fewer edge cases (except it creates a subshell, which can be avoided in some shells by using braces instead of parens)

Thank you. Your comment coalesced a number of things in my mind that I hadn’t grasped properly as a UNIX midwit, especially the braces thing.

Re: An Opinionated Guide to Xargs

#35

I frequently find myself reaching for this pattern instead of xargs: do_something | ( while read -r v; do . . . done ) I’ve found that it has fewer edge cases (except it creates a subshell, which can be avoided in some shells by using braces instead of parens)

For thousands of arguments this sloution is much slower (high CPU usage) than xargs, because either it implements the logic as a shell script (slow) or it runs an external program for each argument (slow).

Re: An Opinionated Guide to Xargs

#36

Wanting verbose logging from xargs, years ago I wrote a script called `el` (edit lines) that basically does `xargs -0` with logging. https://github.com/westurner/dotfiles/blob/develop/scripts/e... It turns out that e.g. -print0 and -0 are the only safe way: line endings aren't escaped: find . -type f -print0 | el -0 --each -x echo GNU Parallel is a much better tool: https://en.wikipedia.org/wiki/GNU_parallel

Yeah but xargs doesn't refuse to run until I have agreed to a EULA stating I will cite it in my next academic paper.

Re: An Opinionated Guide to Xargs

#37
Since the blog author is commenting here, you have this statement part way down your blog:

> That is, grep doesn't support an analogous -0 flag.

However, the GNU grep variant does have an analogous flag:

-z, --null-data

Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option, this option can be used with commands like sort -z to process arbitrary file names.

Re: An Opinionated Guide to Xargs

#39
post #35

I frequently find myself reaching for this pattern instead of xargs: do_something | ( while read -r v; do . . . done ) I’ve found that it has fewer edge cases (except it creates a subshell, which can be avoided in some shells by using braces instead of parens)

For thousands of arguments this sloution is much slower (high CPU usage) than xargs, because either it implements the logic as a shell script (slow) or it runs an external program for each argument (slow).

Sure, if performance matters use xargs. I find this is easier to read and think about.

Re: An Opinionated Guide to Xargs

#40

Wanting verbose logging from xargs, years ago I wrote a script called `el` (edit lines) that basically does `xargs -0` with logging. https://github.com/westurner/dotfiles/blob/develop/scripts/e... It turns out that e.g. -print0 and -0 are the only safe way: line endings aren't escaped: find . -type f -print0 | el -0 --each -x echo GNU Parallel is a much better tool: https://en.wikipedia.org/wiki/GNU_parallel

Yeah but xargs doesn't refuse to run until I have agreed to a EULA stating I will cite it in my next academic paper.

parallel doesn't either, it just nags. I agree about how silly and annoying it is. Imagine if every time the parallel author opened Firefox he got a message reminding him to personally thank me if he uses his web browser for research, or if every time his research program calls malloc he has to acknowledge and cite Ulrich Drepper. Very very silly.

Parallel is the better tool but the nagware impairs its reputation.

Post reply on HN