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:

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?
Maarten BruinsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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.
0
Maarten BruinsAuthor 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.
0
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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...
0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

Maarten BruinsAuthor 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.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
serialbandCommented:
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.
0
Maarten BruinsAuthor 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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.