Practice: Input, Output and Pipes
On this page
An inspection handoff needs only the two middle entries of a six-entry log. A second log mentioned in the handoff is missing. You need to preserve the usable text, keep the missing-file diagnostic separate, and add a review note without replacing the selected entries.
This practice combines B09.1–B09.4. Use the lessons and manuals as needed. Your evidence must show which text reached each destination, rather than relying on everything looking correct in the terminal.
Before you start
Section titled “Before you start”Finish Standard streams through Exit status and conditional execution. Keep your existing B04 lab directory.
Use only disposable ~/lab/b09/practice. Start with this setup once, or use Reset this lesson before repeating it:
mkdir -p ~/lab/b09/practicecd ~/lab/b09/practiceprintf 'run 01: queued\n' > source.logprintf 'run 02: started\n' >> source.logprintf 'run 03: measured\n' >> source.logprintf 'run 04: checked\n' >> source.logprintf 'run 05: sealed\n' >> source.logprintf 'run 06: archived\n' >> source.logThese are fixed synthetic entries, not a record of an actual inspection. Leave missing.log absent. Preserve source.log throughout the task. All destination names below are separate disposable files in this directory.
Your task
Section titled “Your task”Reach these five goals:
- Ask one
catcommand to readsource.logand thenmissing.log. Save its ordinary output incopy.logand its diagnostic inerrors.log. Inspect the command’s status immediately, before reading either destination. Explain which descriptors the two files receive. - Select only entries 03 and 04 from
copy.log. In one pipeline, save those entries tohandoff.logand print their newline count. The count must describe the selected entries, without counting a diagnostic. - Append the line
Reviewedtohandoff.log, preserving both selected entries. Read the result through stdin, without a filename argument to the reading command. - Print
Handoff savedonly ifhandoff.logis a regular file. Inspect that combined line’s status immediately. Explain which command supplies the combined result when the message runs. - Prove that
source.logstill has its original six entries. Explain why the saved diagnostic inerrors.logwas not part of the pipeline’s input.
Before running goal 1, predict: will stdout’s destination contain a diagnostic as an extra line merely because the same cat command reads a missing file? Compare your answer with both saved files.
Run it, then compare with your guess
No. copy.log receives the six source entries through stdout. The diagnostic goes to errors.log through stderr. Redirecting each stream gives the two kinds of text different destinations, even though one command produces both.
Expected evidence
Section titled “Expected evidence”- The two-file read prints no text in the terminal, but its status is nonzero. Its usable text and its diagnostic are in separate files.
- The pipeline prints the count
2and saves only entries 03 and 04, in their original order. - The final handoff contains those two entries followed by
Reviewed, with one newline ending each line. - The conditional prints
Handoff saved; its immediately inspected status is0. - The original log retains all six entries. You can name stdin, stdout, and stderr in the workflow.
Do not use the pipeline’s status alone as evidence that its input was read correctly. B09.4 explains why the final stage’s success does not establish every earlier stage’s success.
Hint for goal 1
Use a different output destination for descriptor 1 and descriptor 2. Inspect $? before running cat to read the saved diagnostic; reading that file would replace the earlier status.
Hint for goal 2
First keep entries 01–04, then keep the final two of that selection. Put tee before wc so the file receives entries rather than a count. This selects a middle portion without a text-search command.
Hint for goals 3 and 4
Use the appending operator for the review line and the input operator for the read. A successful file test can permit a message command with &&.
Hint for goal 5
Count the source’s newlines, then read its text. No command should use source.log as an output destination.
Solution
Section titled “Solution”Solution
Separate usable text and diagnostics.
cat source.log missing.log > copy.log 2> errors.logecho "$?"1The read returns 1 because cat cannot open missing.log. It still copied the first file successfully. Stdout, descriptor 1, went to copy.log; stderr, descriptor 2, went to errors.log. Neither output stream reached the terminal during the read.
Inspect the saved diagnostic:
cat errors.logcat: missing.log: No such file or directoryThis inspection prints the saved text through its own stdout. It does not preserve the previous cat invocation’s nonzero status, which is why the inspection came after echo "$?".
Save and count the middle entries.
head -n 4 copy.log | tail -n 2 | tee handoff.log | wc -l2head reads the named copy and sends its first four lines into the first pipe. tail reads that stdin and retains its last two lines: entries 03 and 04. tee saves both lines and passes the same text to wc. The count describes the stream before the review note is appended.
The diagnostic is in a different file and was never added to copy.log. Each | connects stdout to stdin; it does not fetch other terminal text or read errors.log.
Add the review line, then read through stdin.
printf 'Reviewed\n' >> handoff.logcat < handoff.logrun 03: measuredrun 04: checkedReviewedBash supplies the file as descriptor 0 for this cat; no file argument is passed. >> preserved the selected entries and added the review line at the end. The handoff has three lines after that addition, while the earlier pipeline correctly counted two selected entries.
Check the saved file before printing the message.
test -f handoff.log && printf 'Handoff saved\n'echo "$?"Handoff saved0The file test succeeds, permitting the message. The last executed command on the combined line is the successful printf, which supplies its status 0. The test checks for a regular file; the preceding read is the evidence for its contents.
Verify the source.
wc -l source.log6 source.logcat source.logrun 01: queuedrun 02: startedrun 03: measuredrun 04: checkedrun 05: sealedrun 06: archivedThe count and the six entries match the fixture. Reading it and routing the read’s stdout to a separate destination did not change the source.
Recall check
Section titled “Recall check”Answer before opening each explanation.
1. Two lines appear together in a terminal. Does that prove both came through descriptor 1?
Answer
No. Stdout and stderr normally reach the same terminal. Follow the program’s connections or separate the destinations to establish which stream carries each result.
2. What is the difference between cat handoff.log and cat < handoff.log?
Answer
The first passes a filename argument that cat opens. The second tells Bash to open the file as stdin; cat receives no file argument and reads descriptor 0. Both print the file’s text in this exercise.
3. Why is > unsuitable for adding Reviewed to the selected entries?
Answer
It truncates the existing destination before the command writes, losing the entries. >> preserves the contents and writes at the end. The newline comes from the printf format, not the operator.
4. Does 2>&1 > out.txt make stderr follow stdout into the file?
Answer
No. Stderr takes stdout’s destination at the first redirection. If stdout is then moved to a file, stderr retains its earlier destination. Apply the redirections from left to right.
5. Why must tee handoff.log come before wc -l to save the entries?
Answer
Before wc, the stream contains entries. After wc, stdout contains a count. tee saves the text it receives at its position in the pipeline.
6. A false test permits a successful fallback with ||. What does $? then report, and does it establish that the file exists?
Answer
It reports the last executed command’s status, 0 for the successful fallback. That says the fallback succeeded. It does not turn the false file condition into a true one.
Reset this lesson
Section titled “Reset this lesson”Inspect only this practice directory:
cd ~/lab/b09ls -AR practicerm -r ~/lab/b09/practiceRerun Before you start. If the directory is already absent, skip removal. The setup recreates only the six-entry source, leaving all solution destinations absent.
- Standard connections separate input, ordinary output, and diagnostics, even when a terminal displays both output streams together.
- Redirection chooses file destinations; appending preserves the text already saved.
- Pipelines pass stdout to stdin;
teesaves a copy at its particular stage. - Exit statuses support conditional execution, while saved-text inspection establishes what the files contain.
Next, Searching for files locates names inside a chosen practice tree.
Further reading
Section titled “Further reading”- On your system:
man bash,help test,man cat,man head,man tail,man tee, andman wc. - GNU Bash: Redirections, Pipelines, and Lists of Commands.
- Ubuntu 24.04: tee(1) and wc(1).