
Useless Use Of Cat Isn't Useless DistroTube
video description
Date: 2022-03-30
Related videos
Comments and reviews: 10
Daniel
UUOC is actually a pretty naive concept. Even in scripting, I always prefer reading from stdin over reading from file.
Consider -cat $-infile- - prog1 args - ... - progN args > $-outfile-- this is way more elegant than -prog1 args $-file- - ... - progN args > $-output-- because the input file and the output file are at the beginning and end of the command respectively (as DT pointed out). Makes later refactoring easier also as any program in the chain should be able to easily be swapped out.
Even shell redirection (arguably better than using cat) is not as elegant as you end up with the same issue: prog1 args < $-infile- - ... - progN args > $-outfile-.
For scripting, I don't see any real issue with using cat. Unless there is some huge files involved and you want control over input buffering etc. But that sort of stuff shouldn't be in a shell script in any case. As for programs that do not read from stdin, I typically create a wrapper for them so they do, because input/output files as arguments are a pain:
#!/bin/bash
# wrap prog so that it can take input from stdin
fifo=$(mktemp -u) # unsafe - but you get the point
mkfifo $-fifo-
prog -input $-fifo- &
cat - >$-fifo-
reply
UUOC is actually a pretty naive concept. Even in scripting, I always prefer reading from stdin over reading from file.
Consider -cat $-infile- - prog1 args - ... - progN args > $-outfile-- this is way more elegant than -prog1 args $-file- - ... - progN args > $-output-- because the input file and the output file are at the beginning and end of the command respectively (as DT pointed out). Makes later refactoring easier also as any program in the chain should be able to easily be swapped out.
Even shell redirection (arguably better than using cat) is not as elegant as you end up with the same issue: prog1 args < $-infile- - ... - progN args > $-outfile-.
For scripting, I don't see any real issue with using cat. Unless there is some huge files involved and you want control over input buffering etc. But that sort of stuff shouldn't be in a shell script in any case. As for programs that do not read from stdin, I typically create a wrapper for them so they do, because input/output files as arguments are a pain:
#!/bin/bash
# wrap prog so that it can take input from stdin
fifo=$(mktemp -u) # unsafe - but you get the point
mkfifo $-fifo-
prog -input $-fifo- &
cat - >$-fifo-
reply
Chad
My grampa used to say there is always more than one way to skin a cat. There are several ways to do the same thing or similar things on the cli. It's all a question of style when it comes down to it. I enjoy the methodology of the concepts you freely show us. I'm a Gentoo fan. I do lots of things differently than other people. We however have very similar styles. I absolutely hate moving the curser to the begining of the line. Yes you can add vim functionality to the cli and with a couple presses be at the beginning of a line. But I absolutely embrace the sys admin mantra of being idle as much as possible. Up arrow, backspace is faster than all of that other stuff. I got it the first time you did it. What I do not understand, is if they are such experts, why are they here? Why do they expend their precious life cycle moments imposing their style on everyone? If they are so much superior to something presented. Then why are they even here? I'll never understand vile high and mighty trolls. I ignore these knaves. They are of no consequence or value. They add nothing of value to a topic. Only needless drama.
reply
My grampa used to say there is always more than one way to skin a cat. There are several ways to do the same thing or similar things on the cli. It's all a question of style when it comes down to it. I enjoy the methodology of the concepts you freely show us. I'm a Gentoo fan. I do lots of things differently than other people. We however have very similar styles. I absolutely hate moving the curser to the begining of the line. Yes you can add vim functionality to the cli and with a couple presses be at the beginning of a line. But I absolutely embrace the sys admin mantra of being idle as much as possible. Up arrow, backspace is faster than all of that other stuff. I got it the first time you did it. What I do not understand, is if they are such experts, why are they here? Why do they expend their precious life cycle moments imposing their style on everyone? If they are so much superior to something presented. Then why are they even here? I'll never understand vile high and mighty trolls. I ignore these knaves. They are of no consequence or value. They add nothing of value to a topic. Only needless drama.
reply
Alex
Yeah.. 25 year sysadmin here.
I -ALWAYS- start at cat. it allows me to start transforming data in a clear way. cat xxx - grep blah - grep -v noblah - awk etc
Now, you might say, -yOu dOnT nEEd cat! JuSt grep wItH tHe file name!- The point you're missing is if I want to remove that first grep later on, I then have to transform the whole command and refactor it (moving the file name to the second grep). With the design above I can remove / adjust any component and have the overall command still work. It saves development / brain time when you're filtering / transforming a bunch of data in weird and wonderful ways.
tldr; use the tools you're presented with. There are not -extra cycles- running cat.
reply
Yeah.. 25 year sysadmin here.
I -ALWAYS- start at cat. it allows me to start transforming data in a clear way. cat xxx - grep blah - grep -v noblah - awk etc
Now, you might say, -yOu dOnT nEEd cat! JuSt grep wItH tHe file name!- The point you're missing is if I want to remove that first grep later on, I then have to transform the whole command and refactor it (moving the file name to the second grep). With the design above I can remove / adjust any component and have the overall command still work. It saves development / brain time when you're filtering / transforming a bunch of data in weird and wonderful ways.
tldr; use the tools you're presented with. There are not -extra cycles- running cat.
reply
InnerTranquility
Annoying people for sure. If i would get the chance, i would criticize oh i don't know...how much they open their fridge door. -You are opening too much, letting cold air out...not good for your fridge- or: -You are staying too long on the first gear...you have to switch gear within (-This exact number of seconds-) XD, just to make a point.
My point is, if you are going to nitpick on something like cat in the terminal, why stop there? -You are using 0.26 degrees too much when swinging your leg, while walking, you are wasting leg energy- lol XD.
In the end, i would say to those people: -Put a sock in it-
reply
Annoying people for sure. If i would get the chance, i would criticize oh i don't know...how much they open their fridge door. -You are opening too much, letting cold air out...not good for your fridge- or: -You are staying too long on the first gear...you have to switch gear within (-This exact number of seconds-) XD, just to make a point.
My point is, if you are going to nitpick on something like cat in the terminal, why stop there? -You are using 0.26 degrees too much when swinging your leg, while walking, you are wasting leg energy- lol XD.
In the end, i would say to those people: -Put a sock in it-
reply
Lee
I use cat in my scripts all the time.
I'm sure that some 99% of those times it probably is -useless-
But I really don't care.
In the vast majority of cases the difference it makes is going to be negligable.
Definitely a case of preumature optimisation there.
It probably becomes more important if you are writing a script that will be run by other people and/or in an environment where specific resources (e.g. process count) are at a premium.
But this is not most cases.
reply
I use cat in my scripts all the time.
I'm sure that some 99% of those times it probably is -useless-
But I really don't care.
In the vast majority of cases the difference it makes is going to be negligable.
Definitely a case of preumature optimisation there.
It probably becomes more important if you are writing a script that will be run by other people and/or in an environment where specific resources (e.g. process count) are at a premium.
But this is not most cases.
reply
MrDvbnhbq
Excuse me for giving a piece of advice here. Instead of using UP ARROW and piping, you can certainly use !$, which means -latest argument- or even !:N which means N-th argument, no need to use UP ARROW. The second thing should be mentioned is: you can use vi-mode in your command line, so you can edit the command line easily. You can use external editor such as vim to edit the command line: Ctrl-X,Ctrl-E (you can assign any key binding you like).
reply
Excuse me for giving a piece of advice here. Instead of using UP ARROW and piping, you can certainly use !$, which means -latest argument- or even !:N which means N-th argument, no need to use UP ARROW. The second thing should be mentioned is: you can use vi-mode in your command line, so you can edit the command line easily. You can use external editor such as vim to edit the command line: Ctrl-X,Ctrl-E (you can assign any key binding you like).
reply
Ronald
Piping the output of cat can have safety benefits. Some commands (sed, or gzip for instance) can modify the input file in place. Piping the output of cat into these programs instead can save you from unintentionally losing data if you accidentally use the wrong commandline arguments. Go ahead, ask me how I know -.
reply
Piping the output of cat can have safety benefits. Some commands (sed, or gzip for instance) can modify the input file in place. Piping the output of cat into these programs instead can save you from unintentionally losing data if you accidentally use the wrong commandline arguments. Go ahead, ask me how I know -.
reply
bob
people online have this bad habit of correcting people over the dumbest trivial crap like they are trying to show off how smart they are. hint we don't care as we do not know you if some one is using cat in a way you think is wrong or any other trivial shit keep your opinion to yourself.
reply
people online have this bad habit of correcting people over the dumbest trivial crap like they are trying to show off how smart they are. hint we don't care as we do not know you if some one is using cat in a way you think is wrong or any other trivial shit keep your opinion to yourself.
reply
itech
It's not so much useless as it is wrong.
Cat is for joining files not printing them.
That where the name comes from - conCATenate.
You could use up arrow just as efficiently without cat by utilising the bash vi mode.
reply
It's not so much useless as it is wrong.
Cat is for joining files not printing them.
That where the name comes from - conCATenate.
You could use up arrow just as efficiently without cat by utilising the bash vi mode.
reply
Daniel
Hey DT, you seem to be very annoyed by people-s UUOC comments. Don-t let this bother you, at the Linux community everyone has much to say about literally everything. Too much voiced and sometimes too little actioned.
reply
Hey DT, you seem to be very annoyed by people-s UUOC comments. Don-t let this bother you, at the Linux community everyone has much to say about literally everything. Too much voiced and sometimes too little actioned.
reply
Add a review, comment
Other channel videos















