Linux Labs

Bash: text processing and scripts / Stage 5 of 9 · step 6 of 7

Fix the report script

medium ~25 min
Not running Starting usually ~20 s
Report a problem

Situation

Every night the branches send their uploads to ~/import/inbox, and the importer loads them into the database. Before loading it runs a check and goes on only if the check returned 0:

bash report.sh inbox/*.csv && import-uploads inbox/*.csv

A colleague wrote ~/import/report.sh; the contract is written in the script's header. In the morning you look at the output of the night's check: it has more lines than there are uploads, the empty east.csv passed as OK, and the import started even though the output has a FAIL in it.

Task

Fix ~/import/report.sh so that it keeps its contract:

  1. exactly one line on stdout per argument, in the same order: OK <argument> or FAIL <argument>; the argument exactly as passed, even with a space or an asterisk * in the name; nothing on stderr meanwhile;
  2. OK only if it is a regular file (or a link to one), it can be read and it is not empty; otherwise FAIL;
  3. exit status 1 if at least one file got FAIL, wherever it stands, and 0 if all are OK;
  4. without arguments — empty stdout, the line Usage: report.sh FILE... on stderr and exit status 2.

The check runs the script as you — bash ~/import/report.sh … — on its own files, which are not in inbox. The uploads in inbox are for your experiments: fix the script, not them.

Visible only to you. Saved as you type.

All notes

Lab goals

0 of 4
  • One line per argument with the name exactly as passed, even with a space or an asterisk; stderr is empty — not checked yet
  • OK only for a regular non-empty file that can be read, or for a link to such a file — not checked yet
  • Exit status 1 if any file fails, wherever it stands; exit status 0 if all pass — not checked yet
  • Without arguments — empty stdout, a Usage line on stderr and exit status 2 — not checked yet

Hints

0 of 3 open · they open one at a time
Hint 1
Show the solution after 3 failed checks

Leave the lab?

The machine keeps running for 10 more minutes, then it is removed. Files are lost; completed goals stay.

Loading…