6 ms·
I would also add that not all tools follow the '-' convention, and the ones that do can break in corner-cases. Why invest the effort of remembering how every to
by gwern 17y ago
I would also add that not all tools follow the '-' convention, and the ones that do can break in corner-cases. Why invest the effort of remembering how every tool works and which ones don't, for a obfuscating micro-optimization? /proud useless user of cat
- jrockway 17y agoExactly. I tried to figure out how to make xargs read from a file instead of stdin, and was unsuccessful. My "cat ... |" works every time (at the expense of 5 miliseconds of CPU time. oh noes.)
- camccann 17y agoThere's a nice consistency in doing it that way--it's very easy to think of cat as turning an "inert" file into a stream of data, each program as a function that applies a transformation to a stream with a particular kind of content, and the pipe as function composition (given compatible stream content). A very comfortable programming-like perspective, at least to me. (Pop quiz: What programming idiom closely resembles this "repackage content and apply sequential transformations" approach?)
- silentbicycle 17y agoSpoiler alert: See also Oleg Kiselyov's "Monadic i/o and UNIX shell programming" (http://okmij.org/ftp/Computation/monadic-shell.html http://okmij.org/ftp/Computation/monadic-shell.html).
- theblackbox 17y agoSeriously? You failed at that? I'm intrigued because it always occured to me that you were a particularly fastidious person (with all due respect). I often come accross issues that would seem for all the world to be a pointless artifact of an accepted convention. If this accepted convention is an acquired trait and one learns exclusivly from a limited meduim (irc frustrates me), I find it very curious that they persist as they sometimes act as a difficult barrier for a thorough understanding. This xargs thing, though I know almost nothing about it, seems to parallel similar problems I've encountered and I for one would be interested to hear your take on this (and HN in general). Or just some food for thought for your next blog post maybe?
- jrockway 17y agoWell, my guess is that xargs simply does not accept a filename as the argument. This comes from reading the --help and grepping the manpage for "file". The point was, when I used "cat file |", I knew it was going to work, and it did. When I tried to eliminate the cat by using the program's built-in ability to read a file, I had to read several manpages before determining that it was not possible. All because some tutorial says "cat is useless", when it clearly saved me much more time than the extra CPU time it used. And if you are actually asking; xargs is just a utility to read command-line-arguments from stdin. `echo -e "foo\nbar" | xargs rm' === `rm foo; rm bar' (or depending on the xargs implementation; `rm foo bar'). It kind of reminds me of a "functor map" operation, where stdin is a functor (of command-line arguments), and command-line programs are functions. (I will now mention that xargs also does "join" on the "results" of the "function", which is very ... monad-like. But "Monads are teh awesome and everything is one" is my second-least-favorite Internet meme, so I will spare you. :)
- olefoo 17y agoThe whole 'useless use of cat' meme is basically designed to let Randal Schwartz make fun of people. At one point it may have made a difference, and certainly if you have a shell script that is getting called 10,000 times a day it might make sense to optimise it. But for doing stuff from the commandline; weird shell acrobatics is a premature optimisation.
- jrockway 17y agoThe whole 'useless use of cat' meme is basically designed to let Randal Schwartz make fun of people. So true. My requirement for a shell command is that it be reasonably easy to assemble, return something resembling the correct answer, and that it run in a reasonable amount of time. If your requirements are more strict than those, it's time to write a real program.
- Estragon 17y agoI tried to figure out how to make xargs read from a file instead of stdin, and was unsuccessful. "xargs -a <filename>" First option described in the man page on Red Hat.
- lg 17y agocould be a GNU vs. BSD thing.
- silentbicycle 17y agoBSD xargs doesn't seem to have the -a option.
- jrockway 17y agoMy home machine (Debian GNU/Linux) says "--arg-file", and it looks like a relatively new feature. My RHEL box at work definitely didn't have anything with the word "file" in it.
- gwern 17y agoThe whole thread descending from here is an excellent example of what I mean by corner-cases and memorization. :)
- spudlyo 17y agoSometimes you can use /dev/fd/0, but I agree, it's a stupid micro optimization, and I don't understand why people get so righteous about it. My co-worker would correct you every time he saw it, but then again he's a pedantic geek.
- pyre 17y agoIt's still good to keep it in mind, in case there are instances where it's being executed many times causing a bottleneck (though one-liners aren't really meant to be used in such situations).
- wendroid 17y agozsh and rc provide primitives for this, this is rc diff <{ps} <{sleep 3; ps}
- vidarh 17y agoEquivalent in bash: diff <(ps) <(sleep 3; ps)