>_ IzzyLab

Hands-on task

This task is step 10 of the course “Bash scripts: from a loop to a robust script”

Output to both the screen and a log

medium~7 min

The script /root/job.sh writes to both stdout and stderr. Write a wrapper /root/run_job.sh that runs job.sh so that all of its output — both streams, stdout and stderr — is at the same time shown on screen (on the wrapper's stdout) and written to /root/job.log.

What you'll practise:
  • tee
  • 2>&1
  • stream redirection
  • stdout/stderr
  • pipes

Topic breakdown

A process has two output streams: stdout (1) and stderr (2). The 2>&1 redirection sends stderr to wherever stdout currently points — the streams merge. tee is a "T-splitter": it copies stdin to a file and to its own stdout at the same time, letting you both see the output and save it. The order command 2>&1 | tee file matters: the merge must happen before entering the pipeline.

What's checked

  1. run_job.sh wrapper runs job.sh
  2. both lines visible on screen
  3. both lines written to job.log

What it looks like