Skip to content

Practice: Input, Output and Pipes

Module
Input, Output and Pipes
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
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.

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:

Terminal window
mkdir -p ~/lab/b09/practice
cd ~/lab/b09/practice
printf 'run 01: queued\n' > source.log
printf 'run 02: started\n' >> source.log
printf 'run 03: measured\n' >> source.log
printf 'run 04: checked\n' >> source.log
printf 'run 05: sealed\n' >> source.log
printf 'run 06: archived\n' >> source.log

These 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.

Reach these five goals:

  1. Ask one cat command to read source.log and then missing.log. Save its ordinary output in copy.log and its diagnostic in errors.log. Inspect the command’s status immediately, before reading either destination. Explain which descriptors the two files receive.
  2. Select only entries 03 and 04 from copy.log. In one pipeline, save those entries to handoff.log and print their newline count. The count must describe the selected entries, without counting a diagnostic.
  3. Append the line Reviewed to handoff.log, preserving both selected entries. Read the result through stdin, without a filename argument to the reading command.
  4. Print Handoff saved only if handoff.log is a regular file. Inspect that combined line’s status immediately. Explain which command supplies the combined result when the message runs.
  5. Prove that source.log still has its original six entries. Explain why the saved diagnostic in errors.log was 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.

  • 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 2 and 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 is 0.
  • 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

Separate usable text and diagnostics.

Terminal window
cat source.log missing.log > copy.log 2> errors.log
echo "$?"
Captured output from echo
1

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

Terminal window
cat errors.log
Captured output
cat: missing.log: No such file or directory

This 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.

Terminal window
head -n 4 copy.log | tail -n 2 | tee handoff.log | wc -l
Captured output
2

head 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.

Terminal window
printf 'Reviewed\n' >> handoff.log
cat < handoff.log
Captured output
run 03: measured
run 04: checked
Reviewed

Bash 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.

Terminal window
test -f handoff.log && printf 'Handoff saved\n'
echo "$?"
Captured output
Handoff saved
0

The 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.

Terminal window
wc -l source.log
Captured output
6 source.log
Terminal window
cat source.log
Captured output
run 01: queued
run 02: started
run 03: measured
run 04: checked
run 05: sealed
run 06: archived

The 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.

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.

Inspect only this practice directory:

Terminal window
cd ~/lab/b09
ls -AR practice
Terminal window
rm -r ~/lab/b09/practice

Rerun 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; tee saves 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.