Menu
CoddyTech

Check The Input File

Part of the Bash Scripting section of Coddy's Terminal journey. Lesson 32 of 39.

challenge icon

Challenge

Medium

The report reads the CSV file named by its first argument. Make report.sh check that argument before it prints anything. Keep everything you wrote so far.

  1. Under the comment, add set -euo pipefail so the script stops on errors and unset variables.
  2. When the script gets no argument, print Usage: report.sh FILE to stderr and exit with code 1. Check the count with $#: under set -u, reading a missing $1 is an error.
  3. Save the first argument in a variable named file.
  4. When file is not an existing file, print Error: FILE not found to stderr, with the name in place of FILE, and exit with code 2.
  5. Otherwise print the title === Sales Report === as before.

Try it: run bash report.sh sales.csv. It prints:

=== Sales Report ===

Then run the two error paths with 2>&1, so stderr shows up, and echo "exit $?" after them:

bash report.sh 2>&1; echo "exit $?"
Usage: report.sh FILE
exit 1
bash report.sh missing.csv 2>&1; echo "exit $?"
Error: missing.csv not found
exit 2

Try it yourself

Terminal

All lessons in Bash Scripting

Practice on your own: Terminal playground