Bash: text processing and scripts / Stage 5 of 9 · step 6 of 7
Fix the report script
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:
- exactly one line on stdout per argument, in the same order:
OK <argument>orFAIL <argument>; the argument exactly as passed, even with a space or an asterisk*in the name; nothing on stderr meanwhile; OKonly if it is a regular file (or a link to one), it can be read and it is not empty; otherwiseFAIL;- exit status 1 if at least one file got
FAIL, wherever it stands, and 0 if all areOK; - 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 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