0
0
Bash Scriptingscripting~10 mins

Report generation script in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Report generation script
Start Script
Define Variables
Collect Data
Process Data
Generate Report File
Display/Save Report
End Script
The script starts by setting variables, collects and processes data, then generates and saves a report before ending.
Execution Sample
Bash Scripting
#!/bin/bash
name="Sales Report"
date=$(date +"%Y-%m-%d")
echo "$name - $date" > report.txt
echo "Total Sales: 100" >> report.txt
cat report.txt
This script creates a simple sales report file with a title, date, and total sales, then shows the report.
Execution Table
StepActionVariable ValuesOutputNotes
1Set name variablename='Sales Report'Prepare report title
2Get current datedate='2024-06-15' (example)Store date in variable
3Write title and date to report.txtreport.txt contains 'Sales Report - 2024-06-15'Create report file with header
4Append total sales linereport.txt now has two linesAdd sales data
5Display report.txt contentSales Report - 2024-06-15 Total Sales: 100Show final report
6Script endsAll steps complete
💡 Script ends after displaying the report content.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
nameundefinedSales ReportSales ReportSales ReportSales ReportSales Report
dateundefinedundefined2024-06-152024-06-152024-06-152024-06-15
report.txtfile does not existfile does not existcontains header linecontains header linecontains header + sales linecontains header + sales line
Key Moments - 2 Insights
Why do we use > in the echo command at step 3 and >> at step 4?
At step 3, > creates or overwrites the report file with the header line. At step 4, >> appends the sales line without deleting the existing content, as shown in the execution_table rows 3 and 4.
How does the date command store the current date in the variable?
The date command runs and its output is captured by $(...) and assigned to the variable 'date' at step 2, so the variable holds the current date string, as seen in variable_tracker after step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of report.txt after step 3?
A"Total Sales: 100"
B"Sales Report - 2024-06-15"
C"Sales Report - 2024-06-15\nTotal Sales: 100"
DEmpty file
💡 Hint
Check the 'Output' column for step 3 in the execution_table.
At which step does the script add the total sales line to the report?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in the execution_table for when the sales line is appended.
If the > operator at step 3 was changed to >>, what would happen?
AThe header line would be appended to existing content.
BThe script would fail to write the file.
CThe report file would be overwritten each time.
DThe date variable would change.
💡 Hint
Refer to the key_moments explanation about > vs >> operators.
Concept Snapshot
Report generation script in bash:
- Use variables to store data (e.g., name, date)
- Use > to create/overwrite files, >> to append
- Use echo to write lines to the report
- Use $(command) to capture command output
- Display report with cat or similar
- Script runs step-by-step to build report file
Full Transcript
This bash script starts by setting a variable 'name' to hold the report title. Then it captures the current date into the 'date' variable using the date command. Next, it writes the title and date into a new file called report.txt using the > operator, which creates or overwrites the file. After that, it appends a line with total sales using >> so the existing content is kept. Finally, it displays the content of report.txt using cat. The script ends after showing the report. Variables change as the script runs: 'name' and 'date' hold strings, and the report.txt file grows from empty to containing two lines. Key points include understanding the difference between > and >> for file writing, and how command substitution $(...) captures output into variables. The visual quiz tests understanding of file content after each step and the effect of changing operators.