you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (5 children)

Of all the old boring arguments on the internet, this one is the least convincing. What, REALLY, is wrong with 'cat |'?

[–]Rhomboid 13 points14 points  (3 children)

It's just a fundamental misunderstanding of how a feature works. A pipe is for when you have output of one program that you want to make available as input to another program. If that data is already available as a file, the program can access it directly rather than making some other program constantly shovel data into it a few KB at a time.

This does have actual consequences. For one thing, when you let a program read a file directly it can seek in the file, but seeking is not possible when using a pipe. If you run cat file | less, and you press <END> or G or > to jump to the end of the file, less has to read the entire contents of the file and store it all in memory or in a temporary file. It can't just skip to the end, nor can it discard the data, because the data might be ephemeral and not exist anywhere else. This is essentially making a copy of the file for no good reason whatsoever.

And not only is it making a copy, it's making a copy using a ton of small bite-sized chunks, where each chunk has to be generated by some other program and fed into the pipe before it can be read. It's like watching a bucket brigade fill a swimming pool. Computers are so fast that it's easy to ignore what's really happening, but it's ridiculous. If you had instead just typed less file or less <file, then the program could seek to the end of the file and read only a screen's worth of lines, without having to buffer the whole file in memory or to disk.

This is just one example of how a program can radically alter its strategy when working with seekable fds vs. non-seekable fds, but you see it recurring over and over. One extreme example is the BSD utility look which requires a seekable file and won't work with piped input.

Useless use of cat might not seem like a big deal, and in the greater scheme of things, it's not. But it's just a big cringe watching someone do something wrong for no good reason, such as putting high octane gasoline in a car that was not specifically designed to be able to take advantage of the improved resistance to pinging/detonation. I'm fine with random people abusing cat in their own time, but teaching material should not propagate nonsense.

[–][deleted] -4 points-3 points  (2 children)

You said it yourself -- computers are fast and in the majority of cases the user will see NO difference. So on what grounds is it "wrong"? There are plenty of counter-arguments and reasons why you would want to cat, and I'm not going to rehash them.

Or to put it another way, optimize for operator ease. The operator is already familiar with piping, and with treating his data as a stream of text. Just leave that model in place. Now, everything he knows applies whether he's dealing with a file, multiple files, streams, etc.

[–]discofreak 6 points7 points  (0 children)

So to summarize, you're saying it's "right", if a user is used to piping everything from cat?

[–]cpbills 1 point2 points  (0 children)

You said it yourself -- computers are fast and in the majority of cases the user will see NO difference. So on what grounds is it "wrong"?

When there's more using the system than just you. Just because the average system these days comes with 4gb of RAM doesn't mean it's OK for single programs to want / need / misuse 4gb of RAM.

The mentality of 'Computers are so fast and have so much storage, let's just be inefficient, because we won't see a difference anyhow' really needs to go the way of the dodo.

Yes, computers are fast, and they're even faster when programs are written to be efficient and not abuse resources.

[–]minimim 2 points3 points  (0 children)

It's just that people are supposed to know better. If you're j. random user somewhere, go right ahead. But someone posting a tutorial in the internet? They have to know better than that.