Save The Report
Part of the Bash Scripting section of Coddy's Terminal journey. Lesson 36 of 39.
Challenge
HardAdd an --out FILE option that also saves the report to a file, and end every run with a summary line. Keep everything you wrote so far.
- Change the usage line to
Usage: report.sh FILE [--top N] [--out FILE]. - Add an
--outbranch to thecase: like--top, it callsusagewhen no value follows, otherwise it saves the value in a variableout(start it asout="") and runsshift 2. - Move the lines that print the report (the title, the region lines and the
Totalline) into a function namedreport. - When
outis set, runreport | tee "$out".teecopies its input to the screen and into the file named after it. Without--out, callreporton its own. - Last, print
Summary: S sales in R regions, where S is the number of sales (lines after the header) and R is the number of different regions in the file.--topdoes not change these numbers, and the summary is not saved in the file.
Try it: run bash report.sh sales.csv --top 3 --out top.txt. It prints:
=== Sales Report ===
North 425
East 405
South 335
Total 1350
Summary: 12 sales in 4 regionsThen cat top.txt prints the same lines without the summary:
=== Sales Report ===
North 425
East 405
South 335
Total 1350Try it yourself
All lessons in Bash Scripting
1Arguments And Exit Codes
Script ArgumentsAll The ArgumentsExit CodesChecking ArgumentsRecap - Unit Converter4Strings And Arrays
Slicing StringsTrimming And ReplacingDefault ValuesArraysReading Files Line By LineRecap - CSV Report7Sales Report Project
Project OverviewCheck The Input FileTotals Per RegionSort And FormatA Top OptionSave The ReportPractice on your own: Terminal playground