Good example to explain the purpose of input redirection within a terminal window?

Maarten Bruins
Maarten Bruins used Ask the Experts™
on
I'm reading about "redirection of input" on the internet. I understand what's behind it. For example:

command < file.ext

Open in new window


This is equivalent to:

command 0< file.ext

Open in new window


In general, if you have:

command n< file.ext

Open in new window


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

Open in new window


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

Open in new window


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

Open in new window


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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
You summarized by saying, "Then I'm thinking, "cat file.txt" does the same, so why do we need it?"

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.

Author

Commented:
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.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
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...
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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#a42698776

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.
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
Ok, so for some examples look at the man pages of the "tr" command and the "nc" command. In both commands, the usual way of getting data in to the program is via a redirection of stdin, either from a pipe or the content of a file.
The catmore 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.

Author

Commented:
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

nc host.example.com 1234 < filename.in

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial