Live data from Hacker News

The useful use of cat

mrmr.io

51–60 of 113 posts

Re: The useful use of cat

#51
post #9

Does anyone take the "don't use cat" stuff seriously?

It's more of a symptom of someone who doesn't understand what's actually going on, i.e. cargo cult scripting. Specifically, it indicates some combination of: 1. isn't aware of processes/pipes 2. isn't aware of cat's "primary" functionality 3. isn't aware of shell input redirection 4. isn't aware that shell input redirection can be put before the command

90% of the time, I care more about someone like that understanding my script than the tiny performance impact of an extra process or 'misusing' a function.

Re: The useful use of cat

#52

Earlier quoted context omitted.

When you want to tail a file, but you want it in descending order and you’re not sure how much of it you want: tac file | less

I guess the difference is that I expect things to read top to bottom, so I'd do that as less foo.log G

Fair enough, if that’s how you want it to read. less on its own is certainly quite capable.

Re: The useful use of cat

#53

> Because for me it'd only be a local maxima. Nitpick: this should be „maximum“, maxima is the plural.

Note the 'local'. If the output has several humps, multiple minima/maxima are possible.

Yes, but "it" and "a" in the original sentence are singular.

Re: The useful use of cat

#54

The underlying topic for this is that, most shells treat files/redirections as part of the individual commands and not part of the pipelines. Files should be sometimes treated as commands instead, they plug into the fds of a process the same way as commands do. `cat x | head` attaches `cat x`'s stdout to `head`'s stdin, ` Though I will note that, in a way, bash supports this, as it has a modules system, and one of th…

From the file you linked, the built-in just copies the input files one by one, sequentially, into stdin. All done in chunks of 4096 bytes, a common page size.

Is it as optimal as just attaching the file descriptor? No, because you will have to pipe the contents. Is it optimal (or at least optimal enough)? Considering how pipes work, yes.

Re: The useful use of cat

#56
post #37

if you prefer to use cat just to read your pipeline from left to right, you can just start your command with file redirection. It doesn't have to be at the end of the command. this works in bash too, but if you're using zsh, there're a couple of nice shortcuts on it's own works as more file and > file ... ... ^D allows you to put something into the file quickly without firing up the editor. Though > file will give yo…

Or you could just use cat

Coward, use `tail -c +0` like the rest of us useless command cowboys

Re: The useful use of cat

#57

cat | sudo tee somefile > /dev/null is still my favorite way to paste text into an ssh session and save it to a protected file.

The cat command can be omitted there, as tee reads from standard input by default, even if stdin points to a terminal. I was going to comment an actually useful (and unavoidable in bash) use of cat and ssh, which is to do do nothing with standard input and redirect it to a file:

  file'
And you could just use scp, but I've found clients without scp and servers with the SFTP subsystem disabled.

Re: The useful use of cat

#58
post #39

Earlier quoted context omitted.

If I saw It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.

I don't feel <file is obscure. I've seen shell in that style from coworkers and from open source. Your value judgement against it might just be your experience, rather than something universal.

For sure many people don't know what it does

Furthermore, a little memory lapse or typo, and, oops! >byebyefile

Re: The useful use of cat

#59
I usually start with cat -- later the head doesn't just get replace the cat may too, e.g. replacing it with some command whose output was only temporarily stored in a file for development purposes.

Re: The useful use of cat

#60
post #39

Earlier quoted context omitted.

If I saw It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.

I don't feel <file is obscure. I've seen shell in that style from coworkers and from open source. Your value judgement against it might just be your experience, rather than something universal.

It's definitely more obscure than cat, and even those familiar might be confused with it at the beginning of the line
Post reply on HN