Linux Labs

Bash: text processing and scripts / Stage 5 of 9

Arguments, quotes and file checks

Not running Starting usually ~20 s
Leave the lesson

Step 6 of 7 · lab · 25 minmedium

Fix the report script

The upload check script lets empty files through, cuts names apart and hides failures — bring it up to its contract.

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.

Lab · on your own

No commands are given in this lab: type them in the terminal yourself. Goals are checked on the machine after every command.

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 done yet
  • OK only for a regular non-empty file that can be read, or for a link to such a file — not done yet
  • Exit status 1 if any file fails, wherever it stands; exit status 0 if all pass — not done yet
  • Without arguments — empty stdout, a Usage line on stderr and exit status 2 — not done yet
Ctrl+Enter
Starts the machine node1 · Ubuntu 24.04

The machine is starting

Hints

0 of 3

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 done yet
  • OK only for a regular non-empty file that can be read, or for a link to such a file — not done yet
  • Exit status 1 if any file fails, wherever it stands; exit status 0 if all pass — not done yet
  • Without arguments — empty stdout, a Usage line on stderr and exit status 2 — not done yet
node1 Ubuntu 24.04

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 done yet
  • OK only for a regular non-empty file that can be read, or for a link to such a file — not done yet
  • Exit status 1 if any file fails, wherever it stands; exit status 0 if all pass — not done yet
  • Without arguments — empty stdout, a Usage line on stderr and exit status 2 — not done yet
Loading…