![]() |
Picking things up from the example of:
last | grep chaney | lessYou can break this down into it's three command components:
last | grep chaney | less (cmd1) (cmd2) (cmd3) stdin (closed) stdin (stdout from cmd1) stdin (stdout from cmd2) stdout (stdin cmd2) stdout (stdin cmd3) stdout (screen) stderr (screen) stderr(screen) stderr (screen)Thus the first pipe symbol takes the output (stdout) of cmd1 and turns it into the input (stdin) of cmd2. Likewise, the 2nd pipe takes the output from cmd2 (in this case, all lines from cmd1 that have the word 'chaney' in them) and feeds that to cmd3 on stdin.
Now. Let's look at slide 12, which asks:
What if I want to pipe one command to the next, but redirect the output
(or save the output to a file) along the way?
One example of this would be:
last | grep chaney > /tmp/tmpfile cat /tmp/file | sort | lessThis would sort and display all the lines from last containing the word chaney. We also have the list of lines containing the word chaney in an unsorted form in the file /tmp/tmpfile.
Is there an easier way? Yes. Look at the tee program. (man tee)
last | grep chaney | tee /tmp/tmpfile | sort | lessis not only a Unix command, it's an efficient one! Give it a try and tell me what it does.
Okay, I'll tell you. Tee redirects stdin to a file and prints the output on stdout (thus, it duplicates the data, sending one set to a file and passing the other one straight on.)
Take a look at Homework 5 now, including the third problem. Notice there are three problems (one was not shown during class but is part of the slides.)
Good luck and I'm sorry to have run short on homework-related materials :-)
-dan
Bonus poser:
The cat program displays the contents of a file. More accurately, it lists the contents, without pauses like the less program. Given your new-found knowledge of stdin, stdout and stderr, what does cat really do?
Hint: What's the diff between the following?
last | cat | lessand
last | less
Happy Valley Times Day.