Improve Redirecting Input #454

Open
rgaiacs opened this Issue Sep 29, 2016 · 2 comments

Projects

None yet

3 participants

@rgaiacs
Collaborator
rgaiacs commented Sep 29, 2016

In http://swcarpentry.github.io/shell-novice/04-pipefilter/ we have the following call out box:

Redirecting Input

As well as using > to redirect a program’s output, we can use < to redirect its input, i.e., to read from a file instead of from standard input. For example, instead of writing wc ammonia.pdb, we could write wc < ammonia.pdb. In the first case, wc gets a command line parameter telling it what file to open. In the second, wc doesn’t have any command line parameters, so it reads from standard input, but we have told the shell to send the contents of ammonia.pdb to wc’s standard input.

I think the example is very simple and we should include a second example that covers pipe. My suggestion is to have

Redirecting Input

As well as using > to redirect a program’s output, we can use < to redirect its input, i.e., to read from a file instead of from standard input. For example, instead of writing sort -n lengths.txt, we could write sort -n < lengths.txt. In the first case, sort gets a command line parameter telling it what file to open. In the second, sort doesn’t have any command line parameters, so it reads from standard input, but we have told the shell to send the contents of lengths.txt to sort’s standard input. If we want you can send the output to the head -n 1 with the help of the pipe and we end up with sort -n < lengths.txt | head -n 1.

I try to not increase the size of the lesson but I believe this is a question that one of our students could have.

@rgaiacs rgaiacs added the enhancement label Sep 29, 2016
@shwina
Member
shwina commented Oct 11, 2016

Are pipes/redirection processed left-to-right? Or is there an order of precedence? Are we prepared/do we have time to explain these things?

@jttkim
Contributor
jttkim commented Oct 11, 2016

There is no such thing as precedence in pipes, all processes run concurrently and each one receives the output of its predecessor (in the pipe) as its input. Redirection only applies to the start / end of a pipe (input / output, respectively). Well, to be complete, stdout and stderr can be rdirected separately, but I'd consider this advanced material / no time to discuss in novice lesson.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment