>_ IzzyLab

Hands-on task

This task is step 10 of the course “Clean scripts: shellcheck”

Bring the script to zero warnings

medium~7 min

The script /root/release.sh writes a line of the form <message> at <timestamp> to /tmp/log.txt (the message is the first argument). It has several problems at once that shellcheck complains about. Fix them all so that shellcheck produces not a single warning and the script still writes the message and timestamp to the same file. Open the script in the Editor tab above the terminal, and run shellcheck in the terminal.

What you'll practise:
  • shellcheck
  • quoting variables
  • command substitution
  • cd
  • bash

Topic breakdown

shellcheck produces not one but several independent warnings at once, each with its own SCxxxx code — you need to work through them one by one. Here several typical bash problems are combined: unquoted variable expansions (which break on spaces and globbing), the deprecated form of command substitution, and an unprotected cd that will silently keep running in the wrong directory if the change failed. The idea of the task is that a clean script is protection against edge cases: variables get quoted, substitution is written with modern syntax, and risky changes are insured with an exit-code check.

What's checked

  1. shellcheck without a single warning
  2. script preserves its logic

What it looks like