Maarten Bruins
asked on
Good example to explain the purpose of input redirection within a terminal window?
I'm reading about "redirection of input" on the internet. I understand what's behind it. For example:
This is equivalent to:
In general, if you have:
then the contents of file.ext go to file descriptor "n" as input. I've checked different websites explaining "input redirection". However, the problem is that I didn't see any good example. I'll discuss some examples I saw:
Then I'm thinking, "cat file.txt" does the same, so why do we need it? Another example:
Then I'm thinking, "sort file_list.txt > sorted_file_list.txt" does the same, so why do we need it? Another example:
Then I'm thinking, "more /etc/passwd" does the same, so why do we need it? That's why these are not really good examples in my opinion. What is a good example to explain the purpose of input redirection in a terminal-window?
Probably internally something like "cat file.txt" is being treated as "cat 0< file.txt" (input redirection), but in a terminal-window ... when it really does make sense to use an "input redirection" in a terminal-window? Does someone have a good example?
command < file.ext
This is equivalent to:
command 0< file.ext
In general, if you have:
command n< file.ext
then the contents of file.ext go to file descriptor "n" as input. I've checked different websites explaining "input redirection". However, the problem is that I didn't see any good example. I'll discuss some examples I saw:
cat < file.txt
Then I'm thinking, "cat file.txt" does the same, so why do we need it? Another example:
sort < file_list.txt > sorted_file_list.txt
Then I'm thinking, "sort file_list.txt > sorted_file_list.txt" does the same, so why do we need it? Another example:
more < /etc/passwd
Then I'm thinking, "more /etc/passwd" does the same, so why do we need it? That's why these are not really good examples in my opinion. What is a good example to explain the purpose of input redirection in a terminal-window?
Probably internally something like "cat file.txt" is being treated as "cat 0< file.txt" (input redirection), but in a terminal-window ... when it really does make sense to use an "input redirection" in a terminal-window? Does someone have a good example?
ASKER
That there are different ways to accomplish the same task, I know and I understand. However, that's not what the question is about. I'm asking for a good example that shows that you really need to have "input redirections" in a terminal window.
I don't see any reason to write "cat 0< file.txt" instead of "cat file.txt". This is not easier to read. This post is about "input redirections" and not about pipelines. You're just changing the subject.
I don't see any reason to write "cat 0< file.txt" instead of "cat file.txt". This is not easier to read. This post is about "input redirections" and not about pipelines. You're just changing the subject.
Better to ask a real question... or provide a real example of a task you're trying to accomplish + have people say how they'd solve the task.
You said, "I don't see any reason to write "cat 0< file.txt" instead of "cat file.txt".
Correct. You pick the conjuration which appeals to you most.
Point is, what seems simple to you... well... someone else will have a completely different approach.
For example, I tend to use cat file.txt | grep foo rather than grep foo file.txt because I tend to work with file collections, like log files.
Again, there is no right/wrong... only personal preference...
You said, "I don't see any reason to write "cat 0< file.txt" instead of "cat file.txt".
Correct. You pick the conjuration which appeals to you most.
Point is, what seems simple to you... well... someone else will have a completely different approach.
For example, I tend to use cat file.txt | grep foo rather than grep foo file.txt because I tend to work with file collections, like log files.
Again, there is no right/wrong... only personal preference...
ASKER
Again you're asking for a real question / real example. See: https://www.experts-exchange.com/questions/29120659/Redirecting-output-n-word-when-does-n-greater-than-2-make-sense.html?anchorAnswerId=42698776#a42698776
And about:
Some things are just in general more simple. It's easier to write "1" instead of "5-4+2-1-1". You come up with a pipe example again, but then at least come up with an input-redirect example, because that's what the question is about.
And about:
Point is, what seems simple to you... well... someone else will have a completely different approach.
Some things are just in general more simple. It's easier to write "1" instead of "5-4+2-1-1". You come up with a pipe example again, but then at least come up with an input-redirect example, because that's what the question is about.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The cat & more commands already take a file as input and don't really need the redirects. The redirects are useful for other tasks. It's just a bit stupid to use with cat and more in these specific, simplistic examples that you've shown, except as examples of how redirects work. If you provide something too complex, a beginner to redirects may not be able to grasp the functions when there additional complexity of other functions and may get confused by the extra complexity. They're not meant to be used that way as a general syntax for cat or more, and only used to showcase the redirect so that a beginner can understand it. It allows them to focus on the redirect as the example.
ASKER
Thanks @serialband and @mccarl! Both posts are useful to me. I'll mark the post of mccarl as the correect answer, because the question was about an example in the first place.
See for example: https://www.computerhope.com/unix/nc.htm
See for example: https://www.computerhope.com/unix/nc.htm
nc host.example.com 1234 < filename.in
There are many ways to accomplish the same task in Linux, with slight variations.
For example, grep foo * across many files dumps file name into grep output, where cat * | grep foo just produces the matched lines.
Also cat foo | pipeline tends to be more easily read than complex redirections, so...
Any way you can accomplish something is... the right way...
And, you'll likely choose different variations based on personal preference + also to produce self documenting code.