📖 Scenario: You work in an office where you need to create a simple report from sales data every day. The sales data is stored in a text file with product names and sales numbers. You want to automate making a report that shows only the products with sales above a certain number.
🎯 Goal: Build a bash script that reads sales data from a file, filters products with sales above a threshold, and prints a simple report.
📋 What You'll Learn
Create a sales data file with exact product names and sales numbers
Add a variable for the sales threshold
Use a loop to read the file and select products with sales above the threshold
Print the filtered report showing product names and sales
💡 Why This Matters
🌍 Real World
Automating daily report generation from sales or other data files saves time and reduces errors.
💼 Career
Many jobs require writing scripts to process data files and create reports automatically.
Progress0 / 4 steps
1
Create the sales data file
Create a file called sales.txt with these exact lines: Apple 50 Banana 30 Cherry 20 Date 40 Elderberry 10
Bash Scripting
Hint
Use echo -e with newline characters \n to write multiple lines to sales.txt.
2
Set the sales threshold
Add a variable called threshold and set it to 25.
Bash Scripting
Hint
Use threshold=25 to create the variable.
3
Filter products with sales above threshold
Use a while loop to read each line from sales.txt. Inside the loop, use if to check if the sales number is greater than threshold. If yes, add the product and sales to a variable called report with a newline after each entry.
Bash Scripting
Hint
Use while read product sales; do ... done < sales.txt to read the file line by line.
Use if [ "$sales" -gt "$threshold" ] to compare numbers.
4
Print the filtered report
Print the report variable to show the products with sales above the threshold.
Bash Scripting
Hint
Use printf "%b" "$report" to print the variable with newlines correctly.
Practice
(1/5)
1. What is the main purpose of a report generation script in bash?
easy
A. To automatically collect and save information into a file
B. To compile source code into an executable
C. To delete all files in a directory
D. To open a graphical user interface
Solution
Step 1: Understand the script's goal
A report generation script is designed to gather data and save it for review.
Step 2: Match purpose with options
Only To automatically collect and save information into a file describes collecting and saving information automatically.
Final Answer:
To automatically collect and save information into a file -> Option A
Quick Check:
Report generation = collect and save info [OK]
Hint: Think: What does 'report generation' mean? Collect and save! [OK]
Common Mistakes:
Confusing report generation with compiling code
Thinking it deletes files
Assuming it opens GUIs
2. Which line correctly starts a bash script for report generation?
easy
A. echo "Start script"
B. #!/bin/python
C. #!/bin/bash
D. import bash
Solution
Step 1: Identify the correct shebang for bash
The first line in a bash script must be #!/bin/bash to specify the interpreter.
Step 2: Check other options
Options B, C, and D are either for other languages or commands, not the script start line.
Final Answer:
#!/bin/bash -> Option C
Quick Check:
Shebang for bash = #!/bin/bash [OK]
Hint: Always start bash scripts with #!/bin/bash [OK]
A. The second echo overwrites the file, losing the first line
B. The variable filename is not defined correctly
C. Missing execute permission on the script
D. The cat command is incorrect
Solution
Step 1: Check file redirection operators
The first echo writes "Start report" to the file. The second echo uses > again, overwriting the file.
Step 2: Understand the effect on output
Because of overwrite, the first line is lost. Only the date line remains in the file.
Final Answer:
The second echo overwrites the file, losing the first line -> Option A
Quick Check:
Using > twice overwrites file [OK]
Hint: Use >> to append, not > to avoid overwriting [OK]
Common Mistakes:
Assuming > appends instead of overwriting
Thinking variable is undefined
Blaming cat command
5. You want to create a report script that lists all files in the current directory and their sizes, saving to files_report.txt. Which script snippet correctly does this?
hard
A. #!/bin/bash
ls -l > files_report.txt
B. #!/bin/bash
ls -l >> files_report.txt
ls -a > files_report.txt
C. #!/bin/bash
ls -lh > files_report.txt
D. #!/bin/bash
ls -lh > files_report.txt
wc -l >> files_report.txt
Solution
Step 1: Understand the requirements
The report must list files with sizes and save to files_report.txt.
Step 2: Analyze options for correct commands and output
#!/bin/bash
ls -lh > files_report.txt
wc -l >> files_report.txt uses ls -lh for human-readable sizes and writes to the file. Then it appends the line count with wc -l, adding useful info.
Step 3: Check other options
#!/bin/bash
ls -l > files_report.txt lacks human-readable sizes. #!/bin/bash
ls -l >> files_report.txt
ls -a > files_report.txt overwrites file after appending, losing data. #!/bin/bash
ls -lh > files_report.txt misses line count info.
Final Answer:
Option D correctly lists files with sizes and appends line count to the report -> Option D
Quick Check:
ls -lh for sizes + wc -l appended = correct report [OK]
Hint: Use ls -lh for sizes and append extra info with >> [OK]
Common Mistakes:
Overwriting file after appending
Not using human-readable size option
Missing extra useful info like line count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.